node.js 温斯顿:了解日志记录级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20931089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winston : understanding logging levels
提问by Yanick Rochon
Reading and fiddling with Winston, I'm puzzled as to why the logging levels are ordered as they are and why the transports behave in the way they do (well, at least the Console one). I'd appreciate if someone could, perhaps even thoroughly, with real use case examples, explain why logging with Winston works this way?
阅读和摆弄 Winston,我很困惑为什么日志级别按原样排序,以及为什么传输按它们的方式运行(好吧,至少是控制台)。如果有人可以(甚至可能是彻底地)用真实的用例示例解释为什么使用 Winston 进行日志记录会以这种方式工作,我将不胜感激?
For example, I setup my logger like this :
例如,我像这样设置我的记录器:
var logger = new (winston.Logger)({
levels: winston.config.syslog.levels,
colors: winston.config.syslog.colors,
level: "debug", // I'm not sure what this option even does here???
transports: [
new (winston.transports.Console)({
colorize: true,
handleExceptions: true,
json: false,
level: "debug"
})
]
});
So, if I do logger.debug("Test");, then it will log debug: Test, fine. But if I do logger.info("Test");, then nothing happens.
所以,如果我这样做logger.debug("Test");,那么它会记录debug: Test,很好。但是如果我这样做了logger.info("Test");,那么什么都不会发生。
The problem I have is that, If I want to log to the console eveverythingbutdebugmessages, what do I do? ... or even debugandinfomessages, but log everything else?
我的问题是,如果我想登录到控制台eveverything但debug消息,我该怎么办?...甚至debug和info消息,但记录其他所有内容?
Coming from a Java world, using the standard loggers, I am used to having debugbeing more "fine grained" than warnand the loggers worked backwards; setting the logging level to info, for example, did log everything but debug(or something).
来自 Java 世界,使用标准记录器,我习惯于debug比warn记录器更“细粒度”,并且记录器向后工作;info例如,将日志记录级别设置为 ,确实记录了除debug(或其他)之外的所有内容。
Also, what if I'd like a logger to log only error, warningand infomessages, how would I do that with Winston?
另外,如果我想什么记录只记录error,warning以及info短信,我会怎么做,与温斯顿?
* EDIT *
* 编辑 *
Apparently, this order of level is unique to winston.config.syslog.levels. So the only question remaining is : "Is it possible to, somehow, restrict a transport to a very specific logging level only?"
显然,这个级别的顺序是winston.config.syslog.levels. 所以剩下的唯一问题是:“是否有可能以某种方式将传输限制到非常特定的日志记录级别?”
采纳答案by kumarharsh
As per the documentation, you can set your own Logging levels, 0 being lowest, and associate colours with it. Now, if you don't want to log the lowest level, just set the levelproperty to the corresponding level. By default, the console logger has it's level set to info
根据文档,您可以设置自己的日志记录级别,0 是最低级别,并将颜色与其关联。现在,如果您不想记录最低级别,只需将level属性设置为相应级别即可。默认情况下,控制台记录器的级别设置为info
So, here is an example:
所以,这是一个例子:
logger = new (winston.Logger)({
levels: {
'info': 0,
'ok': 1,
'error': 2
}
transports: [
new (winston.transports.ConsoleTransport)(silent: options.silent, level: 'ok')
]
});
回答by mostafa.mortazavi
var logger = new (winston.Logger)({
levels: {
'info': 0,
'ok': 1,
'error': 2
},
colors: {
'info': 'red',
'ok': 'green',
'error': 'yellow'
},
transports: [
new (winston.transports.Console)({level:'info',colorize: true})
]
});
logger.log('info',"This is info level");
logger.info("This is info level");

