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.
Winston is the most popular logging library for Node. js. It aims to make logging more flexible and extensible by decoupling different aspects such as log levels, formatting, and storage so that each API is independent and many combinations are supported. It also uses Node.
You can do something like: const { createLogger, format, transports } = require('winston'); const { splat, combine, timestamp, label, printf, simple } = format; const logger = createLogger({ format: combine( label({ label: 'CUSTOM', message: true }), timestamp(), simple() ), transports: [new transports.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With