简体   繁体   English

nodejs - 将appjs控制台传递给文件

[英]nodejs - pipe appjs console to a file

I try to pipe appjs console to a file with this code: 我尝试使用以下代码将appjs控制台传递给文件:

var fs = require('fs');
var logStream = fs.createWriteStream(__dirname+ '/log.txt', { flags: 'a' });
process.stdout.pipe(logStream);
process.stderr.pipe(logStream);
console.log("test");

It creates an empty file, but nothing more... With node.exe the "test" goes into the console, not into the log file. 它创建一个空文件,但仅此而已......使用node.exe,“test”进入控制台,而不是进入日志文件。 The platform is win32, but I don't think it counts. 该平台是win32,但我认为这不重要。

What's the problem with the code? 代码有什么问题?

conclusion: 结论:

Stdout, stderr and a file write stream are all sink type endpoints, so I cannot bind them together. stdout,stderr和文件写入流都是接收器类型端点,因此我无法将它们绑定在一起。 I need to replace stdout and stderr with douplex mock streams so I will be able to bind these mock streams both to the original sinks and the log sink. 我需要用douplex模拟流替换stdout和stderr,这样我就可以将这些模拟流绑定到原始接收器和日志接收器。 I am not sure whether console.log and console.error will be affected by replacing the streams with the mechanism supernova suggested, I'd rather use a dedicated logger, which uses the console instead of this workaround. 我不确定是否会通过使用超新星机制替换流来影响console.log和console.error,我宁愿使用专用的记录器,它使用控制台而不是这种解决方法。

you have to define getters for process.stdin, process.stdout and process.stderr 你必须为process.stdin,process.stdout和process.stderr定义getter

var fs = require("fs")
  , errlog = fs.createWriteStream("./err.log", { flags: 'a' })

process.__defineGetter__("stderr", function(){
  return errlog 
})

process.stderr.write("test")

this should work 这应该工作

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

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