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

时间:2021-07-08 16:55:02

My node.js application 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.update that erases/replaces a console line rather than creating a new line?

是否有可能做一个删除/替换控制台行而不是创建新行的console.update?

4 个解决方案

#1


38  

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

#2


14  

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/bento for more usage examples.

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

#3


14  

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 + '%');
}

#4


7  

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

#1


38  

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

#2


14  

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/bento for more usage examples.

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

#3


14  

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 + '%');
}

#4


7  

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