小编Wan*_*hap的帖子

如何在进程退出之前强制 nodejs winston 登录到文件

我正在使用winston 3来记录我的数据。但有时它在进程退出之前没有记录错误。

以下过程将退出,不登录logfile.log

const winston = require('winston');

winston.add(new winston.transports.File({
    filename: 'logfile.log'
}));

winston.info('please log me in');
process.exit(1);
Run Code Online (Sandbox Code Playgroud)

尝试的解决方案之一是使用回调:

const winston = require('winston');

const logger = new winston.createLogger({
    new winston.transports.File({
    filename: 'logfile.log'
}));

winston.info('please log me in', () => {
    process.exit(1);
});

setTimeout(() => {}, 100000000000); // pause the process until process.exit is call
Run Code Online (Sandbox Code Playgroud)

但是 Winston 3.x 有回调问题,所以上面的代码不起作用,进程不会退出。

我正在寻找可行的解决方案?任何帮助将不胜感激。我的操作系统是Ubuntu 16.04 , Node 10.17

编辑 1: 我也有 tryPrabhjot Singh Kainth的建议使用finish事件来触发进程退出: …

node.js winston

6
推荐指数
1
解决办法
2294
查看次数

标签 统计

node.js ×1

winston ×1