My application uses Winston for logging. The application crawls data from a few different data sources. Before starts the crawling, I instantiated a number of Winston logging objects with different log files specified and pass these objects to different crawling functions, where some functions are shared. I was expecting each crawling job will log into their own log files, but it ends up with a mixed logging. Crawling job A's data maybe logged into job B's file and vice versa. Anyone has encountered similar issue before?
Winston config template is as below. The $ will be replaced with a job name.
config.log = {
console : {timestamp:true, level:'silly'},
file : {
filename: 'E:/Logs/$.log'
, json:false, maxsize:10485760, maxFiles:'10'
, timestamp:true, level:'silly'
},
exception : {
filename: 'E:/Logs/$exception.log'
, json:false, maxsize:10485760, maxFiles:'10'
, timestamp:true, level:'info'
}
};
The function to instantiate Winston logger
var winston = require('winston');
function getLogger(caller) {
var logconfig = JSON.parse(JSON.stringify(config.log));
logconfig.file.filename = logconfig.file.filename.replace('$', caller);
logconfig.exception.filename = logconfig.exception.filename.replace('$', caller);
var logger = new (winston.Logger)({
transports: [
new winston.transports.Console(logconfig.console),
new winston.transports.File(logconfig.file)
],
exceptionHandlers: [
new winston.transports.File(logconfig.exception)
]
});
return logger;
}
module.exports = getLogger;
Code to start jobs
//Main job logger
var joblogger = require('/logging.js')('job');
//Inividual crawling jobs
var joblists = [JobA, JobB, JobC];
joblists.forEach(function(j) {
var logger = require('/logging.js')(j.name);
j.run(logger);
});