【问题标题】:is there any standard warning (stdwarn) like stdout and stderr?是否有像标准输出和标准错误这样的标准警告(stdwarn)?
【发布时间】:2018-02-21 10:53:05
【问题描述】:

是否有任何标准警告 (stdwarn),例如 stdoutstderr

实际上,在我的 node.js 应用程序中,我使用 console.error() 进行安慰的所有内容都添加到文件 log_err.log

console.warn(), console.info() and console.log() 这样的所有其他内容都添加到log_std.log 文件中,但我希望在不同的日志文件中显示警告消息。

类似于:

node server.js > log_std.log 2> log_err.log 3> log_warn.log

这里>用于标准日志,2>我们用于标准错误打印到文件中

3> 在这里什么都没有,但我想知道在 log_warn.log 文件中是否有类似的语法来打印警告消息。

【问题讨论】:

  • 您是指process.emitWarning() 发出的警告还是未处理的承诺拒绝警告?
  • 实际上在我的节点应用程序中,我使用控制台打印日志、错误和警告。喜欢:console.log('simple log'); console.warn('warning'); console.error('error');

标签: node.js linux shell console stdout


【解决方案1】:

不,没有stdwarn。但是你应该自己做。它可以让您更好地控制您的日志。使用 log warnerror 方法创建您自己的记录器,并决定您想用它做什么。

var Log = {
    log: function(msg) {
        // output message to your log
        // it can be file, or syslog drain etc.
    },
    warn: function(msg) {
        //...
    },
    error: function(msg) {
        //...
    }
};

此外,您可以捕获 uncaughtExceptionunhandledRejection 事件并将它们发送到您的日志:

process.on('uncaughtException', err => { Log.error(err.stack); process.exit(1)});
process.on('unhandledRejection', (reason, p) => Log.warn(reason.stack || reason));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多