Python日志记录-DEBUG下方是否有内容? [英] Python logging - Is there something below DEBUG?

查看:91
本文介绍了Python日志记录-DEBUG下方是否有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他一些技术中,我们偶尔使用低于DEBUG的日志级别,我认为这被称为详细".我意识到,需要这样一个级别是非常主观的.但是在我看来,仅拥有INFO和DEBUG并不够.我们有时需要记录非常垃圾邮件(比调试更多的垃圾邮件).在实践中,我们会在不启用此功能的情况下生成内部版本,但在某些情况下,我们会在产品全部安装到某些质量检查设置中并跟踪错误等之后启用这种级别的日志记录.

In some other technologies we occasionally used a log level below DEBUG that I believe was called "verbose". I realize that the need for such a level is very subjective. But in my mind "just" having INFO and DEBUG isn't really enough. We had times where something very spammy (more spammy than debug) needed to be logged. In practice we'd produce builds without this turned on but in a few occasions we'd enable this level of logging after the product was all installed on some QA setup, while tracking down a bug, etc.

是否可以使用标准的python日志记录库(简便或其他方式)记录低于DEBUG级别的内容?

Is there any way (easy or otherwise) to log something below the DEBUG level using the standard python logging library?

在temp.py文件中,我可以执行以下操作:

In a temp.py file I can do the following:

logging.addLevelName(5,"verbose")
VERBOSE = 5

logger = logging.getLogger("foo")
logger.setLevel(VERBOSE)
logger.log(VERBOSE,"blah!")

当我在IDE中运行temp.py(并记录到stdout)时,此方法有效,但我们的实际守护程序使用标准的文件/字典配置语法来设置日志记录,但是我看不到任何方法表明应该将5级设置为用于守护程序.

This works when I run temp.py within my IDE (and logs to stdout) but our real daemons use the standard file/dictionary configuration syntax to setup logging and I don't see any way to indicate that level 5 should be used for the daemon.

我在追逐一些不太可行的东西吗?

Am I chasing something that's not really feasible?

对于那些可能想知道为什么我需要除DEBUG之外的内容的人来说,这是因为偶尔会出现的日志记录类型非常频繁(可能是一个内部循环),即使在DEBUG上,我通常也不想看到这种记录,但是在某些生产系统中,偶尔启用它可能会有所帮助,而无需在源代码中添加更多日志记录并重新部署等.

For those who might wonder why I'd need anything lower than DEBUG, it's for the occasional type of logging that might occur very frequently (maybe an inner loop) that I wouldn't normally want to see even at DEBUG but on some production system it might be helpful to enable it once in awhile without needing to add more logging to the source code and re-deploy, etc.

EDIT1-显然,日志记录库允许自定义级别.由于DEBUG为10级,因此在1..9范围内有空间.如果我定义了一个自定义级别(例如上面的示例代码中的代码),我想我真正的问题是如何从json日志配置文件中启用该级别的日志记录?

EDIT1 - Clearly the logging library allows for custom levels. Since DEBUG is level 10, there's room somewhere in the 1..9 range. If I define a custom level (such as in the sample code above), I guess my real question is how do I enable that level of logging from the json log configuration file?

EDIT2-如果不是因为我们需要/使用json配置文件(这是我无法更改的要求),那么以下内容将起作用:

EDIT2 - The following would work if it weren't for the fact that we need/use json configuration files (a requirement that I cannot change):

import logging

logging.basicConfig(filename='example.log',level=5)
VERBOSE = 5
logging.addLevelName(5,"verbose")
logger = logging.getLogger("bar")
logger.log(VERBOSE,"blah!")

EDIT3-弄清楚了...的呼叫

EDIT3 - Figured it out... The call to

logging.addLevelName(5,"VERBOSE")

至关重要.我只是没有正确的地方.就我而言,我只需要在对记录库dictConfig(...)调用的调用之前 进行上述调用.完成此操作后,我便可以进入我们的日志配置文件,并将其(在文件处理程序和根目录上)放到VERBOSE上.

is critical. I just didn't have it in the right place. In my case I just needed to make the above call takes place before the call to the logging libraries dictConfig(...) call. After I did this I was then able to go into our log configuration file and bump things down (on both the file handler and the root) to VERBOSE and it worked.

当然,log语句本身并不十分优雅,因为您调用:

Granted, the log statement itself isn't exactly elegant because you call:

self.logger.log(VERBOSE,"Something very spammy")

而不是

self.logger.verbose("Something very spammy")

但是我真的不想修改任何记录器库代码(在那儿做完,穿上T恤).

But I really didn't want to modify any logger library code (been there, done that, have the t-shirt).

谢谢!

对于那些认为只需要DEBUG的人,您将拥有更多的权力:)

And to those who think nothing lower than DEBUG is needed, more power to you :)

推荐答案

DEBUG 是日志记录模块('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')提供的最低级别.它们的数值在这里: http://docs.python.org/howto/logging .html#logging-levels

DEBUG is the lowest level out of the ones provided by the logging module: ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). Their numeric values are here: http://docs.python.org/howto/logging.html#logging-levels

您可以创建自定义级别(尽管文档中指出应该很少是必要的,甚至是不希望的).如果要添加级别,该技术很简单:

You can create custom levels (though the docs say that that should rarely be necessary and may even be undesirable). If you want to add a level, the technique is simple:

>>> logging.addLevelName(5, "VERBOSE")

尽管可以添加自定义级别,但是添加一些可以提供更好控制级别的过滤器可能是更好的方法.

Eventhough you can add a custom level, it may be a better approach to add some filters that provide a finer level of control.

这篇关于Python日志记录-DEBUG下方是否有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆