除第一天外,如何使用Winston每日轮换日志

lar*_*ron 13 logging node.js winston

我需要每天轮换日志,除了当天的文件.我正在使用winstonwinston-daily-rotate-files库.

在以下示例中,仅在我第一次执行节点时生成文件"info.log.2016-08-09".

但是,我真的需要生成文件"info.log",在这一天之后,应该重命名为"info.log.2016-08-09",并为当天创建一个新的"info.log".我明白这是其他应用程序中的正常行为.

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        name: 'cronInfo',
        filename:  path.join(__dirname,"log", "info.log"),
      level: 'info',
       timestamp: function(){                
        return utils.formatDate(new Date(), "yyyy-mm-dd'T'HH:MM:ss.l'Z'")
      },
      formatter: function(options) {
          return  options.timestamp() +' ['+ options.level.toUpperCase() +'] '+ (undefined !== options.message ? options.message : '') +
               (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );          
      },
      json:false,
      datePattern:".yyyy-MM-dd"
    })
   ]
});
 
Run Code Online (Sandbox Code Playgroud)

los*_*rje 0

另一种没有数据边界问题的方法就是使用:

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        // your definition of rotate file
      })
   ]
});
Run Code Online (Sandbox Code Playgroud)

然后为 info.log 创建一个链接到当天文件的符号链接。最后你可以使用:

transport.on('rotate', function(oldFilename, newFilename) {
    fs.symlinkSync(newFilename, 'info.log');
});
Run Code Online (Sandbox Code Playgroud)

在旋转时更新符号链接。

如果您的日志文件特别大,这也将节省一些磁盘空间。