I was using DailyRotateFile for a while and got happy with the results. But now I'm trying to include some keywords specifically for our business like environment, transaction Ids and alike. I just know we have the "meta" parameter to provide some extra information but I really want a custom formatter because otherwise developers need to take care about this extra parameter every time they want to log some stuff.
Said so, I added this code to my logger setup.
const transports = [
new (winston.transports.DailyRotateFile)({
filename: './logs/scrapper.log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
colorize: true,
level: config.get('logging.level'),
zippedArchive: false,
timestamp: function(){
return Date.now();
},
formatter: function(options){
return options.timestamp() + '-' + process.env.NODE_ENV + '-
message:' + (options.message ? options.message : '')
}
}),
new (winston.transports.Console)({
timestamp: function() {
return Date.now();
},
formatter: function(options) {
return options.timestamp() + '-' + process.env.NODE_ENV + '-
message: ' + (options.message ? options.message : '')
}
})
];
Funny thing is it works perfectly fine with Console transport, but that is not the case with DailyRotateFile's one. I'm still getting default format on log files. Digging into library code I came out to think that options.formatter parameter is not getting all way through to common module on winston library itself. Any thought?
Set JSON as false to use custom formatter in daily rotate file
new (winston.transports.DailyRotateFile)({
filename: './logs/scrapper.log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
colorize: true,
json:false, //Setting JSON as false
level: config.get('logging.level'),
zippedArchive: false,
timestamp: function(){
return Date.now();
},
formatter: function(options){
return options.timestamp() + '-' + process.env.NODE_ENV + '-message:' + (options.message ? options.message : '')
}
})