Winston 记录到文件有延迟,但记录到控制台则没有 (Node.js)

Nyx*_*nyx 5 javascript ubuntu logging node.js winston

我有一个 Node.js 应用程序,它运行 shell 命令execSync,并使用它winston来将消息记录到控制台和文件传输。

问题:通过跟踪日志文件,我们可以看到正在记录到控制台的消息在脚本完成运行之前不会立即记录到文件中。

有没有办法让 Winston 立即将日志刷新到文件中?


使用节点12.14.1,温斯顿3.2.1,Ubuntu 18.04.3

在跑步的中间

(左边) tail -f logs/test.log

(正确的) node test.js

在此输入图像描述

跑步结束

(左边) tail -f logs/test.log

(正确的) node test.js

在此输入图像描述


Node.js代码

const winston = require('winston');
const { execSync } = require('child_process');

const options = {
    file: {
        level: 'debug',
        filename: `./logs/test.log`,
    },
    console: {
        level: 'debug'
    }
};

let logger = winston.createLogger({
    transports: [
        new winston.transports.File(options.file),
        new winston.transports.Console(options.console),
    ]
});

logger.info('First log message')

for (const name of ['jack','ryan','tom']) {
    const output = execSync('sleep 1');    // simulate a command that does some work
    logger.debug(name)
}
Run Code Online (Sandbox Code Playgroud)