node.js - 是否有任何标准警告(stdwarn),如 stdout 和 stderr?

标签 node.js linux shell console stdout

有没有像stdoutstderr这样的标准警告(stdwarn)?

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

和其他所有东西,如 console.warn()、console.info() 和 console.log() 正在添加到 log_std.log 文件,但我想要警告不同日志文件中的消息。

类似于:

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

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

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

最佳答案

不,没有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));

关于node.js - 是否有任何标准警告(stdwarn),如 stdout 和 stderr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48904066/

相关文章:

javascript - NodeJS 和 Gulp 中的 Gzip(双 gzip)

node.js - sailsjs 的 CSRF token 存储

node.js - 在 Node 中创建 REST API 时,如何将来自对外部网站的请求的 http 响应流式传输到原始 api 调用?

ruby - RVM PATH 未正确设置

linux - 如何在系统中显示有关 DVB(数字视频广播)卡的信息

linux - 如何在 linux 中一步将子文件夹中的 jpg 文件转换为每个 pdf 文件?

El Capitan 下的 Python "app transport security"错误

python - 为什么 Popen.terminate() 在 Windows 上终止使用命令解释器 (cmd.exe) 运行的子进程?

node.js - React-native如何在键盘上方向上移动文本字段

linux - DNS 查找 - 确定目标服务器