温斯顿:了解日志记录级别

编程入门 行业动态 更新时间:2024-10-24 10:14:23
本文介绍了温斯顿:了解日志记录级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在阅读和摆弄Winston时,我感到困惑的是,为什么记录级别按原样排序,为什么传输按照它们的行为方式运行(嗯,至少是控制台一个).如果有人可以甚至可以通过真实的用例示例来说明问题,我将不胜感激,为什么解释使用Winston进行日志记录的工作原理如此?

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?

例如,我这样设置记录器:

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" }) ] });

因此,如果我执行logger.debug("Test");,则它将记录debug: Test,很好.但是,如果我执行logger.info("Test");,则什么也不会发生.

So, if I do logger.debug("Test");, then it will log debug: Test, fine. But if I do logger.info("Test");, then nothing happens.

我的问题是,如果我想登录控制台一切 但是 debug消息,我该怎么办? ...甚至debug 和 info消息,但还要记录其他所有内容?

The problem I have is that, If I want to log to the console eveverything but debug messages, what do I do? ... or even debug and info messages, but log everything else?

来自Java世界,使用标准的记录器,我习惯使debug比warn更细粒度",并且记录器向后工作.例如,将日志记录级别设置为info确实记录了除debug(或其他内容)以外的所有内容.

Coming from a Java world, using the standard loggers, I am used to having debug being more "fine grained" than warn and the loggers worked backwards; setting the logging level to info, for example, did log everything but debug (or something).

此外,如果我希望记录器仅记录error,warning和info消息,我该如何使用Winston做到这一点?

Also, what if I'd like a logger to log only error, warning and info messages, how would I do that with Winston?

显然,此级别的顺序对于winston.config.syslog.levels是唯一的.因此剩下的唯一问题是:是否可以通过某种方式将传输限制为仅特定的日志记录级别?"

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?"

推荐答案

按照文档,您可以设置自己的日志记录级别,最低为0,并为其关联颜色.现在,如果您不想记录最低级别,只需将level属性设置为相应级别.默认情况下,控制台记录器的级别设置为info

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 level property to the corresponding level. By default, the console logger has it's level set to info

所以,这是一个示例:

logger = new (winston.Logger)({ levels: { 'info': 0, 'ok': 1, 'error': 2 } transports: [ new (winston.transports.ConsoleTransport)(silent: options.silent, level: 'ok') ] });

更多推荐

温斯顿:了解日志记录级别

本文发布于:2023-11-24 04:20:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级别   温斯顿   日志

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!