In my node app I am using "Winston" Logging to print the errors in a separate file.
I followed this tutorial.
When I tried to access the logger from other files....
My code:
var winston = require('winston');
var fs = require('fs');
fs.mkdir('./logs', function(err) {
if (err) throw err;
});
// Define levels to be like log4j in java
var customLevels = {
levels: {
debug: 0,
info: 1,
warn: 2,
error: 3
},
colors: {
debug: 'blue',
info: 'green',
warn: 'yellow',
error: 'red'
}
};
// create the main logger
var logger = new(winston.Logger)({
level: 'debug',
levels: customLevels.levels,
transports: [
// setup console logging
new(winston.transports.Console)({
level: 'info', // Only write logs of info level or higher
levels: customLevels.levels,
colorize: true
}),
// setup logging to file
new(winston.transports.File)({
filename: './logs/project-debug.log',
maxsize: 1024 * 1024 * 10, // 10MB
level: 'debug',
levels: customLevels.levels
})
]
});
// create the data logger - I only log specific app output data here
var datalogger = new (winston.Logger) ({
level: 'info',
transports: [
new (winston.transports.File) ({
filename: './logs/project-data.log',
maxsize: 1024 * 1024 * 10 // 10MB
})
]
});
// make winston aware of your awesome colour choices
winston.addColors(customLevels.colors);
var Logging = function() {
var loggers = {};
// always return the singleton instance, if it has been initialised once already.
if (Logging.prototype._singletonInstance) {
return Logging.prototype._singletonInstance;
}
this.getLogger = function(name) {
return loggers[name];
}
Logging.prototype.get = this.getLogger;
loggers['project-debug.log'] = logger;
loggers['project-data.log'] = datalogger;
Logging.prototype._singletonInstance = this;
};
new Logging(); // I decided to force instantiation of the singleton logger here
logger.info('Logging set up OK!');
module.exports = Logging;
Error is throwing:
Logging() is undefined.
The tutorial seems to have a bunch of errors. I got it working by calling
var logger = logging.Logging().get('project-debug.log');
Notice the .log
in the argument. The argument has to match one of the names defined in the loggers
array.