简体   繁体   English

Python:仅当发生错误时,如何记录debug +消息,否则返回info +

[英]Python: How to log debug+ messages only when an error occures, info+ otherwise

My objective: 我的目标:

  • If no error happened in the method, log only messages of level INFO and higher. 如果该方法未发生错误,则仅记录INFO或更高级别的消息。
  • If an error message appears, log all messages of level DEBUG and higher prior to the error as well. 如果出现错误消息,请在发生错误之前记录所有DEBUG和更高级别的消息。

I found logging.handlers.MemoryHandler class: https://docs.python.org/2/library/logging.handlers.html#memoryhandler 我找到了logging.handlers.MemoryHandler类: https : //docs.python.org/2/library/logging.handlers.html#memoryhandler

It partly fits my purpose. 这部分符合我的目的。 It buffers all messages and flushes them whenever an error-level message appears. 它会缓冲所有消息并在出现错误级别的消息时刷新它们。 But if there is no error-message, it won't log anything at all. 但是,如果没有错误消息,它将根本不会记录任何内容。

I want my app to still log INFOs and WARNINGs if there were no error. 如果没有错误,我希望我的应用仍记录信息和警告。

What's the best way to implement that? 最好的实现方法是什么?

The way I see it you can achieve something similar by configuring two loggers. 我的看法是,可以通过配置两个记录器来实现类似的目的。

  1. INFO-Logger to log only INFO level logging INFO-Logger仅记录INFO级别的日志记录
  2. DETAIL-Logger to log in DEBUG mode DETAIL-Logger以调试模式登录

As an example see the documentation 作为示例,请参见文档

You can decide how to log them, same file (maybe) or separate files (my-simple.log and my-detailed.log) 您可以决定如何记录它们,相同文件(也许)或单独文件(my-simple.log和my-detailed.log)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将调试消息记录到Python中的系统日志中 - Unable to log debug messages to the system log in Python 如何在/ var / log / messages中调试“检测到的未处理的Python异常” - How to debug 'detected unhandled Python exception in' /var/log/messages INFO服务器记录器收集错误日志消息 - INFO serverity logger collects ERROR log messages 如何将调试级别记录到文件,但仅在python屏幕上记录信息级别 - how to logging debug level to file but only info level on screen in python 在MAC OSX上使用Python的SyslogHandler时,DEBUG和INFO消息在哪里? - Where do DEBUG and INFO messages go when using Python's SyslogHandler on MAC OSX? Django:如何将日志级别设置为INFO或DEBUG - Django: how to set log level to INFO or DEBUG 谷歌云中的日志记录信息/调试消息 apache 光束 python sdk - logging info/debug messages in google cloud apache beam python sdk 如何使用Automator运行python脚本以查看调试/错误消息? - How to run python script with Automator to see debug/error messages? 在调试模式下逐步执行时没有错误,但在AttributeError情况下没有 - No error when stepping through in debug mode but AttributeError otherwise 调试模式下的 Flask 应用程序不会显示信息或调试日志消息 - Flask app in debug mode won't show info or debug log messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM