简体   繁体   English

如何在Windows上刷新console.log输出

[英]How to flush console.log output on Windows

I have a Node.js program with a long process with some internal logging: 我有一个带有一些内部日志记录的长进程的Node.js程序:

console.log(msg)

When I run on Windows Node.js, the message is not displayed in real time - I get a large number of messages in the same late time. 当我在Windows Node.js上运行时,消息不会实时显示 - 我在相同的晚期收到大量消息。

When I use "console.warn" instead of "console.log", the messages display earlier, but still not in real time. 当我使用“console.warn”而不是“console.log”时,消息会更早显示,但仍然不能实时显示。

Is there a way to tell Node.js to automatically flush the console buffer after each log message? 有没有办法告诉Node.js在每条日志消息后自动刷新控制台缓冲区?

It turns out that console.log is asynchronous in Windows. 事实证明, console.log在Windows中是异步的。 In linux/unix it is blocking, so it's not buffering and needing a flush, putting your console.logs on the event queue. 在linux / unix中它是阻塞的,因此它不会缓冲并需要刷新,将console.logs放在事件队列中。

One alternative may be process.stdout 一种替代方案可以是process.stdout

Edit: ok that's not a solution. 编辑:好的,这不是解决方案。 Someone else may have a better solution, but you can either use one of the more comprehensive logging libraries or populate your console.log with more info that tells you when and where the log occurred so you can correlate them. 其他人可能有更好的解决方案,但您可以使用一个更全面的日志记录库,也可以使用更多信息填充您的console.log,告诉您日志发生的时间和位置,以便您可以关联它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM