node.jsloggingwinston

How to daily rotate logs using Winston except the first day


I need to rotate the logs daily except the file for current day. I'm using winston and winston-daily-rotate-file libraries.

In the following example, a file "info.log.2016-08-09" is generated just the first time I execute node.

But, I really need to generate the file "info.log", and after this day, should be renamed to "info.log.2016-08-09", and create a new "info.log" for the current day. I understant that this is the normal behaviour in other applications.

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"
    })
   ]
});
 


Solution

  • Another approach without data boundary issues would be just to use:

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

    And then create a symlink for info.log that links to the current day's file. Finally you can use:

    transport.on('rotate', function(oldFilename, newFilename) {
        fs.symlinkSync(newFilename, 'info.log');
    });
    

    to update the symlink when it rotates.

    If your log files are particularly large this will save some disk space as well.