Python:警告和日志记录详细限制

发布于 2024-08-04 12:54:22 字数 382 浏览 3 评论 0原文

我想统一我的应用程序的整个日志记录工具。任何警告都会引发异常,接下来我捕获它并将其传递给记录器。但问题是:记录中是否有静音设施?有时记录器变得过于冗长。有时由于警告过于嘈杂,警告是否有任何详细限制?

http://docs.python.org/library/logging.html

http://docs.python.org/library/warnings.html

I want to unify the whole logging facility of my app. Any warning is raise an exception, next I catch it and pass it to the logger. But the question: Is there in logging any mute facility? Sometimes logger becomes too verbose. Sometimes for the reason of too noisy warnings, is there are any verbose limit in warnings?

http://docs.python.org/library/logging.html

http://docs.python.org/library/warnings.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-08-11 12:54:22

不仅有日志级别,而且有一个非常灵活的< href="http://docs.python.org/library/logging.html#configuring-logging" rel="nofollow noreferrer">配置它们的方法。如果您使用命名的logger对象(例如,logger =logging.getLogger(...)),那么您可以适当地配置它们。这将允许您在逐个子系统的基础上配置详细程度,其中子系统由日志记录层次结构定义。

另一种选择是使用 logging.Filter警告过滤器 来限制输出。我以前没有使用过这种方法,但看起来它可能更适合您的需求。

阅读PEP-282,了解有关 Python 的良好散文描述<代码>日志记录包。我认为它比模块文档更好地描述了功能。

澄清后编辑

您也许能够使用基于 logging.Logger 并使用 logging.setLoggerClass() 注册的自定义类来处理此日志记录部分。听起来您确实想要类似于 syslog 的“最后一条消息重复了 9 次”的内容。不幸的是我不知道在任何地方都有这样的实现。您可能想查看 twisted.python.log 支持这个功能。

Not only are there log levels, but there is a really flexible way of configuring them. If you are using named logger objects (e.g., logger = logging.getLogger(...)) then you can configure them appropriately. That will let you configure verbosity on a subsystem-by-subsystem basis where a subsystem is defined by the logging hierarchy.

The other option is to use logging.Filter and Warning filters to limit the output. I haven't used this method before but it looks like it might be a better fit for your needs.

Give PEP-282 a read for a good prose description of the Python logging package. I think that it describes the functionality much better than the module documentation does.

Edit after Clarification

You might be able to handle the logging portion of this using a custom class based on logging.Logger and registered with logging.setLoggerClass(). It really sounds like you want something similar to syslog's "Last message repeated 9 times". Unfortunately I don't know of an implementation of this anywhere. You might want to see if twisted.python.log supports this functionality.

物价感观 2024-08-11 12:54:22

从你提到的来源开始。
有日志级别,请明智地使用;-)

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

from the very source you mentioned.
there are the log-levels, use the wisely ;-)

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}
冷夜 2024-08-11 12:54:22

如果您计划从某个盲目错误处理程序进行所有日志记录调用,而该处理程序对引发错误的代码一无所知,这将是一个问题,这就是您的问题听起来的样子。您将如何决定哪些日志记录调用进行,哪些不进行?

更标准的做法是使用此类块来恢复(如果可能),并记录错误(实际上,如果这是一个您没有专门准备的错误,您想了解它;使用高级别)。但不要依赖这些块来获取所有状态/调试信息。最好在代码到达错误处理程序之前用日志记录调用来填充代码。这样,您可以在系统未发生故障时观察有关系统的有用运行时信息,并且可以进行不同严重程度的日志记录调用。例如:

import logging
from traceback import format_exc
logger = logging.getLogger() # Gives the root logger.  Change this for better organization
# Add your appenders or what have you
def handle_error(e):
    logger.error("Unexpected error found")
    logger.warn(format_exc()) #put the traceback in the log at lower level
    ... #Your recovery code
def do_stuff():
    logger.info("Program started")
    ... #Your main code
    logger.info("Stuff done")
if __name__ == "__main__":
    try:
        do_stuff()
    except Exception,e:
        handle_error(e)

This will be a problem if you plan to make all logging calls from some blind error handler that doesn't know anything about the code that raised the error, which is what your question sounds like. How will you decide which logging calls get made and which don't?

The more standard practice is to use such blocks to recover if possible, and log an error (really, if it is an error that you weren't specifically prepared for, you want to know about it; use a high level). But don't rely on these blocks for all your state/debug information. Better to sprinkle your code with logging calls before it gets to the error-handler. That way, you can observe useful run-time information about a system when it is NOT failing and you can make logging calls of different severity. For example:

import logging
from traceback import format_exc
logger = logging.getLogger() # Gives the root logger.  Change this for better organization
# Add your appenders or what have you
def handle_error(e):
    logger.error("Unexpected error found")
    logger.warn(format_exc()) #put the traceback in the log at lower level
    ... #Your recovery code
def do_stuff():
    logger.info("Program started")
    ... #Your main code
    logger.info("Stuff done")
if __name__ == "__main__":
    try:
        do_stuff()
    except Exception,e:
        handle_error(e)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文