Hi,
I'm trying to use Winston to automate my logging for an internship's project. I've just noticed that the debug logs don't show up, though I use the same method as for 'info', 'warn' and 'error' (which are perfectly displayed for what I need).
Here is my code:
var winston = require('winston');
var dateFormat = require('dateformat');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function() {
var now = new Date();
return dateFormat(now, "isoDateTime");
},
formatter: function(options) {
// Return string will be passed to logger.
return options.timestamp() + options.level.toUpperCase() + ' ' + (options.message == undefined ? "-" : options.message);
}
})
]
});
logger.setLevels(winston.config.syslog.levels);
winston.addColors(winston.config.syslog.colors);
module.exports = {
/**
* Log debuging messages
* @param {String} message
*/
debug: function(message) {
logger.log('debug','Data to log.');
},
/**
* Log information messages
* @param {String} message
*/
info: function(message) {
logger.log('info','Data to log.');
},
/**
* Log warning messages
* @param {String} message
*/
warn: function(message) {
logger.log('warn','Data to log.');
},
/**
* Log error messages
* @param {String} message
*/
error: function(message) {
logger.log('error','Data to log.');
}
};
Runnung a test.js like:
var logger = require('./logger.js');
logger.debug('test');
logger.info('test');
logger.warn('test');
logger.error('test');
Gives me only the following output:
2016-04-08T18:47:26+0200 INFO test
2016-04-08T18:47:26+0200 WARN test
2016-04-08T18:47:26+0200 ERROR test
Am I doing something wrong or does it comes from Winston?
Infos:
The probem is the same on windows and linux:
$ node -v
v0.10.42
> node -v
v5.10.1
Replacing logger.setLevels(winston.config.syslog.levels); with winston.setLevels(winston.config.syslog.levels); doesn't change anything.
And adding a console.dir(winston.config.syslog.levels) gives me the following object (that seems good to me...):
{
levels:
{
emerg: 0,
alert: 1,
crit: 2,
error: 3,
warning: 4,
notice: 5,
info: 6,
debug: 7
},
colors:
{
emerg: 'red',
alert: 'yellow',
crit: 'red',
error: 'red',
warning: 'red',
notice: 'yellow',
info: 'green',
debug: 'blue'
}
}
I'm sorry, I've just found that the mimum level for logs was something to set.
It seems that the default level for syslog is 'info', so 'debug' entries were not displayed.
So it was not an issue, just a lack of reading ^^
I hope that my mistake will at least help others realize this detail faster...
@Cobraeti thanks for the note! I just had the same problem, and your comment gave me the light in the darkness:)
this.level = options.level || 'info';
I'm also facing same issue, can I get solution.
Hi @Spandana-Gajangi ,
It's in the README, here is the direct link: https://github.com/winstonjs/winston#using-logging-levels
Depending on your project, see one of the two last examples of this section to set this when configuring your transport or at runtime ;)
I'm trying this
const logger = new (winston.Logger)({
level:{
'debug':0,
'error':1,
'info':2,
},
transports: getTransports(level),
});
logger.log(level, msg, logEntry);
}
It's not working
This test works on my Docker environment (see this page, I use alpine tag so Node version 9.2.1):
var winston = require('winston');
const logger = new (winston.Logger)({
transports: [
new winston.transports.Console({ level: 'debug' })
]
});
logger.log('debug',"Test");
The NPM page helped me write this short code, hope this could help...
Thank you @Cobraeti, your response helped me a lot.
You're welcome, hope it will now also be referenced for others facing the same problem ;)
Most helpful comment
I'm sorry, I've just found that the mimum level for logs was something to set.
It seems that the default level for syslog is 'info', so 'debug' entries were not displayed.
So it was not an issue, just a lack of reading ^^
I hope that my mistake will at least help others realize this detail faster...