How to rotate logs daily with Winston except the first day

I need to rotate the logs daily, except for the file for the current day. I use winston and the winston-daily-rotate-file libraries .

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

But I really need to generate the "info.log" file, and after that day it should be renamed to "info.log.2016-08-09" and create a new "info.log" for today. I understand that this is normal behavior 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" }) ] }); 
+8
logging winston
source share
1 answer

Well, the workaround could be one transport for info.log

Similar:

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

And then configure some cron to remove info.log at midnight, i.e. node-schedule

However, with this approach, there may be a slight inconsistency, if something is missing after midnight, it can write several lines to info.log, which belongs to the next day, and then is deleted, so info.log may be incomplete.

But all magazines with this info.log.2016-08-09 format remain complete and unaffected.

Therefore, keep in mind that the probability of incomplete info.log for one day is very low. (however, you can create a more advanced check that not only deletes the file, but looks if there is a new day file, and if so, it looks inside and then only removes the logs from previous days from info.log and does not delete them all right away)

+2
source share

All Articles