Winston logger not write to file

2019-06-03 14:16发布

I want to use winston logger in node app for log error to file. But winston not write to file.

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

 logger.error('test');

file somefile.log is created but empty.

1条回答
Summer. ? 凉城
2楼-- · 2019-06-03 14:53

You need to define a level in the constructor.

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
查看更多
登录 后发表回答