I have decided to use winston
for logging in my node
project. I am developing this application on windows platform but ultimatly it would be on a linux platform.
Here is the code I have used to configure the logger.
/* Custom logging module
* to write appropriate logs into log files as well as console.
*/
var winston = require( 'winston' );
winston.setLevels({
info: 0,
error: 1,
warning: 2,
audit: 3
});
winston.addColors({
info: 'blue',
error: 'red',
warning: 'yellow',
audit: 'green'
});
var logger = new ( winston.Logger )({
levels: {
info: 0,
error: 1,
warning: 2,
audit: 3
},
transports : [
new (winston.transports.Console)({
level: 'info',
prettyPrint: true,
colorize: true,
silent: false,
timestamp: true
}),
new (winston.transports.File)({
name : 'infoLogger',
filename : './logs/info-log.log',
prettyPrint: false,
level: 'info',
silent: false,
colorize: true,
timestamp: true,
maxsize: 40000,
maxFiles: 10,
json: false,
tailable : true
}),
new (winston.transports.File)({
name : 'errorLogger',
filename : './logs/error-log.log',
prettyPrint : false,
level : 'error',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
}),
new (winston.transports.File)({
name : 'warningLogger',
filename : './logs/warning-log.log',
prettyPrint : false,
level : 'warning',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
}),
new (winston.transports.File)({
name : 'auditLog',
filename : './logs/audit-log.log',
prettyPrint : false,
level : 'audit',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
})
],
colors: {
info: 'blue',
error: 'red',
warning: 'yellow',
audit: 'green'
}
});
module.exports = logger;
And I am using fallowing code to log into files.
logger.info( 'Winstom Log Info' );
logger.error( 'Winstom Log Error' );
logger.warning( 'Winstom Log Waring' );
logger.audit( 'Winstom Log Audit' );
However, there is no colors used in logged messages. And the logged files are not exactly logging their own levels. For example,
error-log.log
contains fallowing messages.
2015-05-23T13:34:32.479Z - error: Winstom Log Error
2015-05-23T13:34:32.480Z - warning: Winstom Log Waring
2015-05-23T13:34:32.481Z - audit: Winstom Log Audit
2015-05-23T13:45:33.433Z - error: Winstom Log Error
2015-05-23T13:45:33.436Z - warning: Winstom Log Waring
2015-05-23T13:45:33.436Z - audit: Winstom Log Audit
2015-05-23T13:56:50.660Z - error: Winstom Log Error
2015-05-23T13:56:50.661Z - warning: Winstom Log Waring
2015-05-23T13:56:50.661Z - audit: Winstom Log Audit
2015-05-23T14:00:04.319Z - error: Winstom Log Error
2015-05-23T14:00:04.319Z - warning: Winstom Log Waring
2015-05-23T14:00:04.319Z - audit: Winstom Log Audit
Error log is catching up logs for warning and audit logs as well. Same is the case for warning logs, it is catching up logs for audit logs as well.
2015-05-23T13:34:32.480Z - warning: Winstom Log Waring
2015-05-23T13:34:32.481Z - audit: Winstom Log Audit
2015-05-23T13:45:33.436Z - warning: Winstom Log Waring
2015-05-23T13:45:33.436Z - audit: Winstom Log Audit
2015-05-23T13:56:50.661Z - warning: Winstom Log Waring
2015-05-23T13:56:50.661Z - audit: Winstom Log Audit
2015-05-23T14:00:04.319Z - warning: Winstom Log Waring
2015-05-23T14:00:04.319Z - audit: Winstom Log Audit
Right now, only audit log and info log are working as expected. Audit containing only audit logs and info log containing every one of them.
What is it that causing this error ? And please also enlighten me on any of the best practices on logging.
Based on your log levels configured here:
levels: {
info: 0,
error: 1,
warning: 2,
audit: 3
},
Winston is behaving exactly as it should. That is, if you have a log file transport set to listen to log level error
(1), it will capture all logs at that level and higher. Maybe you meant to structure your levels like this?
levels: {
info: 0,
warning: 1,
error: 2,
audit: 3
},
In this structure, your error log file would only capture error
and audit
logs.
As far as your colors not working, are you using cmd.exe
by any chance? Console colors might not work as they should there. Friends don't let friends use cmd.exe on windows. cmder
is a much better option, especially for node.js development on windows: