Winston记录器无法写入文件

时间:2017-07-06 18:25:33

标签: javascript node.js winston

我想在winston应用中使用node记录器来记录日志错误。但是winston没有写入文件。

var logger = new (winston.Logger)({
             transports: [
                new (winston.transports.Console)(),
                new (winston.transports.File)({ filename: 'somefile.log' })
              ]
            });

 logger.error('test');

文件somefile.log已创建但为空。

1 个答案:

答案 0 :(得分:2)

您需要在构造函数中定义一个级别。

var tsFormat = () => (new Date()).toLocaleTimeString();
var log = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            timestamp: tsFormat,
            colorize: true,
            level: 'info'
        }),
        new (winston.transports.File)({
            filename: 'somefile.log',
            level: 'error'
        })
    ]
});


log.error('test'); // writes to log and console out
log.info('testing info'); // writes just in console