Node.js console.log - 是否可以更新一行而不是创建一个新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17309749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:54:03  来源:igfitidea点击:

Node.js console.log - Is it possible to update a line rather than create a new line?

javascriptnode.js

提问by JVG

My node.jsapplication has a lot of console logs, which are important for me to see (it's quite a big app so runs for a long time and I need to know that things are still progressing) but I'm ending up with thousands of lines of console logs.

我的node.js应用程序有很多控制台日志,这对我来说很重要(它是一个相当大的应用程序,所以运行了很长时间,我需要知道事情仍在进行中)但我最终得到了数千行控制台日志。

Is it somehow possible to do a console.updatethat erases/replaces a console line rather than creating a new line?

是否有可能执行console.update擦除/替换控制台行而不是创建新行的操作?

回答by michelek

Try playing with process.stdout methods instead on console:

尝试在控制台上使用 process.stdout 方法:

process.stdout.write("Hello, World");
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write("\n"); // end the line

回答by Bruno Peres

Following @michelek's answer, you can use a function somewhat like this:

按照@michelek 的回答,您可以使用类似这样的函数:

function printProgress(progress){
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(progress + '%');
}

回答by Thank you

Sure, you can do this using a module I helped create: fknsrs/jetty

当然,你可以使用我帮助创建的模块来做到这一点:fknsrs/jetty

Install via

通过安装

npm install jetty

Here's a usage example

这是一个使用示例

// Yeah, Jetty!
var Jetty = require("jetty");

// Create a new Jetty object. This is a through stream with some additional
// methods on it. Additionally, connect it to process.stdout
var jetty = new Jetty(process.stdout);

// Clear the screen
jetty.clear();

// write something
jetty.text("hello world");
jetty.moveTo([0,0]);
jetty.text("hello panda");

Jetty is not super useful when used on it's own. It is much more effective when you build some abstraction on top of it to make your jetty calls less verbose.

Jetty 单独使用时并不是很有用。当您在它之上构建一些抽象以使您的码头调用不那么冗长时,它会更有效。

Check out how I use jetty in fknsrs/bentofor more usage examples.

查看我如何在fknsrs/bento 中使用 jetty 以获取更多使用示例。

回答by SpliFF

To write a partial line.

写一个部分行。

process.stdout.write("text");
process.stdout.write("more");
process.stdout.write("\n"); // end the line

If the volume of output is the real issue then you'll probably to rethink your logging. You could use a logging system that allows selective runtime logging to narrow your output to what you need.

如果输出量是真正的问题,那么您可能会重新考虑您的日志记录。您可以使用允许选择性运行时日志记录的日志记录系统将输出范围缩小到您需要的范围。

// The sections we want to log and the minimum level
var LOG_LEVEL = 4;
var LOG_SECTIONS = ["section1","section2","section3"];

function logit(msg, section, level) {
  if (LOG_SECTIONS.indexOf(section) > -1 && LOG_LEVEL >= level) {
    console.log(section + ":" + msg);
  }
}

logit("message 1", "section1", 4); // will log
logit("message 2", "section2", 4); // will log
logit("message 3", "section3", 2); // wont log, below log level
logit("message 4", "section4", 4); // wont log, not in a log section

回答by Maksim Shamihulau

if you see stdout exceptions like TypeError: process.stdout.clearLine is not a functionin Debug Console window of Visual Studio Code (or Webstorm), run the app as external terminal application instead of internal console. The reason is that Debug Console window is not TTY (process.stdout.isTTYis false). Therefore update your launch configuration in launch.jsonwith "console": "externalTerminal"option.

如果您TypeError: process.stdout.clearLine is not a function在 Visual Studio Code(或 Webstorm)的调试控制台窗口中看到 stdout 异常,请将应用程序作为外部终端应用程序而不是内部控制台运行。原因是调试控制台窗口不是 TTY(process.stdout.isTTY是假的)。因此,launch.json使用"console": "externalTerminal"选项更新您的启动配置。

回答by Bart Theeten

Just use \r to terminate your line:

只需使用 \r 来终止您的线路:

process.stdout.write('text\r');

Here's a simple example (wall clock):

这是一个简单的例子(挂钟):

setInterval(() => process.stdout.write(`clock: ${new Date()}\r`), 1000);