有什么标准警告(stdwarn)像stdout和stderr?

时间:2018-02-21 10:53:06

标签: node.js linux shell console stdout

是否有任何标准警告(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>在这里没什么,但我想知道是否有类似的syntex在 log_warn.log 文件中打印警告消息。

1 个答案:

答案 0 :(得分:0)

不,没有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));
相关问题