Winston logger not write to file

2019-06-03 14:09发布

问题:

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:

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