温斯顿轮流写入多个文件

时间:2017-04-13 16:12:09

标签: javascript node.js winston

所以我使用winston-daily-rotate-file。 在app.js中我有:

var logger = require('./logger');

然后:

logger.info("logging to info");
logger.error("logging to error");

在logger / index.js中我有:

var error_transport = new winston.transports.DailyRotateFile({
    filename: '../logs/error',
    datePattern: 'yyyy-MM-dd.',
    prepend: true,
    level: 'error',
name: 'error'
});
var info_transport = new winston.transports.DailyRotateFile({
    filename: '../logs/info',
    datePattern: 'yyyy-MM-dd.',
    prepend: true,
    level: 'info',
    name: 'info'
});

var logger = new (winston.Logger)({
    transports: [
        error_transport,
        info_transport
    ]
});

module.exports = logger;

文件

会发生什么
logs/DATE_error

包含:

logging to error    

但是:文件

logs/DATE_info 

包含:

logging to info
logging to error

为什么info文件还包含错误日志?

1 个答案:

答案 0 :(得分:2)

您可以在此处阅读有关Winston的日志记录级别:Logging Levels

信息包含错误日志的基本原因是"级别"具有更高优先级的号码将始终记录任何级别的消息"低于"他们优先考虑。请参阅Using Logging Levels

下的示例



  var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Console)({ level: 'warn' }),
      new (winston.transports.File)({ filename: 'somefile.log', level: 'error' })
    ]
  });
  logger.debug("Will not be logged in either transport!");
  logger.transports.console.level = 'debug';
  logger.transports.file.level = 'verbose';
  logger.verbose("Will be logged in both transports!");