JVG*_*JVG 63 javascript node.js
我的node.js应用程序有很多控制台日志,这对我来说很重要(它是一个非常大的应用程序,因此运行了很长时间,我需要知道事情仍在进行中)但我最终会有数千行控制台日志.
是否可能以某种方式console.update擦除/替换控制台线而不是创建新线?
mic*_*lek 102
尝试在控制台上使用process.stdout方法:
process.stdout.write("Hello, World");
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write("\n"); // end the line
Run Code Online (Sandbox Code Playgroud)
Bru*_*res 34
按照@ michelek的回答,您可以使用如下函数:
function printProgress(progress){
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(progress + '%');
}
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 15
当然,你可以使用我帮助创建的模块来做到这一点:fknsrs/jetty
通过安装
npm install jetty
Run Code Online (Sandbox Code Playgroud)
这是一个用法示例
// 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");
Run Code Online (Sandbox Code Playgroud)
Jetty在使用它时并不是非常有用.当你在它上面构建一些抽象来使你的码头调用更简洁时,它会更有效.
查看我如何在fknsrs/bento中使用jetty以获取更多用法示例.
小智 13
只需使用 \r 来终止您的线路:
process.stdout.write('text\r');
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子(挂钟):
setInterval(() => process.stdout.write(`clock: ${new Date()}\r`), 1000);
Run Code Online (Sandbox Code Playgroud)
Spl*_*iFF 11
写一个部分线.
process.stdout.write("text");
process.stdout.write("more");
process.stdout.write("\n"); // end the line
Run Code Online (Sandbox Code Playgroud)
如果输出量是真正的问题,那么您可能会重新考虑您的日志记录.您可以使用允许选择性运行时日志记录的日志记录系统,将输出范围缩小到所需的范围.
// 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
Run Code Online (Sandbox Code Playgroud)
小智 9
我们可以使用日志更新
const logUpdate = require('log-update');
logUpdate('this will be gone');
logUpdate('this will stay');
Run Code Online (Sandbox Code Playgroud)
如果您TypeError: process.stdout.clearLine is not a function在 Visual Studio Code(或 Webstorm)的调试控制台窗口中看到 stdout 异常,请将应用程序作为外部终端应用程序而不是内部控制台运行。原因是调试控制台窗口不是 TTY(process.stdout.isTTY是假的)。因此,launch.json使用"console": "externalTerminal"选项更新您的启动配置。
其中,@michelek 的回答确实有效。但是,当您开始使用此功能时,当输出重定向到文件或者您处于调试器中或在 Linux 屏幕会话中运行等时,您可能会遇到异常问题。您可能会看到诸如process.stdout.clearLine is not a function.
因此,至少添加一个测试来检查输出是否为“TTY”并且能够执行“clearLine()”和“cursorTo()”等操作:
if (process.stdout.isTTY) {
process.stdout.write("Hello, World");
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write("\n"); // end the line
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31718 次 |
| 最近记录: |