Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
    })
   ]
});
 
like image 540
larrytron Avatar asked Aug 09 '16 14:08

larrytron


People also ask

How do I change the log level in Winston?

If you are using the default logger, you can adjust the log levels like this: const winston = require('winston'); // ... winston.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.

What is Winston logger?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).


2 Answers

Well one workaround can be to have one more transport to info.log

Just like this :

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        //your definition of rotate file
      }),
    new (winston.transports.File)({ filename: 'info.log' })
   ]
});

And then set up some cron to delete info.log at midnight, i.e. node-schedule

However, with this approach there can be little inconsistency, if something is run through midnight, it can log few lines into info.log which belongs to next day and then it is deleted, therefore info.log can be incompleted.

But all the logs with this info.log.2016-08-09 format remains full and unaffected.

So it is to consider if very small chance to have incomplete info.log for one day is acceptable. (however you can create more advanced checker that does not just delete file, but looks if exist file of new day and if so, it looks whats inside and then delete only the logs from previous days from info.log and it does not delete it all at once)

like image 137
libik Avatar answered Sep 18 '22 15:09

libik


For those who may still be looking for the right way, this ability was added in v.4.1.0.

Just use createSymlink and symlinkName options:

new DailyRotateFile({
    ...
    createSymlink: true,
    symlinkName: 'info.log',
});
like image 23
lub0v Avatar answered Sep 18 '22 15:09

lub0v