【问题标题】:Method Log of the Node class Console is not wirting to the fileNode类控制台的方法日志没有写入文件
【发布时间】:2015-12-08 06:55:06
【问题描述】:

我关注那篇文章https://nodejs.org/api/console.html如何将输出写入文件

var output = fs.createWriteStream('./stdout.log');
var errorOutput = fs.createWriteStream('./stderr.log');
var logger = new Console(output, errorOutput);
logger.log('log my stuff to the file');

但我看不到任何写入文件的内容。为什么? 我只能看到空文件 0 字节。

我刚刚在我的代码中发现了一些谜团。 当我评论该代码时,记录器正在工作,但是当我取消注释该代码时,记录器不工作:

var exec = require('child_process').exec, child;
child = exec(cmd,
             function (error, stdout, stderr) { 

                 console.log(cmd);

                 if (error !== null) {
                      console.log(error);
                 }
             })

我的代码没有任何问题/错误,一切正常。但是当我打电话给child = exec...时,登录不起作用@

Console 上的文档说:

全局控制台是一个特殊的控制台,它的输出被发送到 process.stdout 和 process.stderr

所以在我的情况下,我正在运行 child = exec... 它正在运行另一个进程,所以它可能正在破坏 Console.Log() ... 有什么想法吗?

【问题讨论】:

  • 它有效,它在 stdout.log 中打印“将我的东西记录到文件中”

标签: javascript node.js logging


【解决方案1】:

完整的脚本应该是这样的:

var Console = require('console').Console,
    fs = require('fs'),
    output = fs.createWriteStream('./stdout.log'),
    errorOutput = fs.createWriteStream('./stderr.log'),
    logger = new Console(output, errorOutput);

logger.log('log my stuff to the file');

【讨论】:

  • 有什么问题,你可以的,function test () { logger.log('log my stuff to the file'); }
  • 是的,我只是想了解为什么有时它不写任何东西,但有时却写了
  • 如果你可以发布一些代码,我可以帮忙,否则不行。
  • 如果我调试了一些代码,我发现当我评论了其中的一部分时,logger.log 正在工作。否则不是。但我需要执行该代码。刚刚用我正在谈论的代码更新了问题。
猜你喜欢
  • 2013-08-29
  • 2021-07-26
  • 2018-03-15
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
相关资源
最近更新 更多