如何将 INFO 和 DEBUG 日志消息发送到 stdout 并将更高级别的消息发送到 stderr
有没有一种简单的方法可以使用 python 的日志模块将 DEBUG 或 INFO 级别的消息以及更高级别的消息发送到不同的流?
无论如何,这是一个好主意吗?
Is there an easy way with python's logging module to send messages with a DEBUG or INFO level and the one with a higher level to different streams?
Is it a good idea anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
除了实现之外,我确实认为使用 python 中的日志记录功能输出到终端是一个好主意,特别是因为您可以添加另一个处理程序来另外记录到文件。如果将 stdout 设置为 INFO 而不是 DEBUG,您甚至可以包含用户通常不会在日志文件中看到的其他 DEBUG 信息。
Implementation aside, I do think it is a good idea to use the logging facilities in python to output to the terminal, in particular because you can add another handler to additionally log to a file. If you set stdout to be INFO instead of DEBUG, you can even include additional DEBUG information that the user wouldn't standardly see in the log file.
只是为了方便起见,将所有内容与格式化程序一起添加到一个包中:
我的用例是将标准输出重定向到数据文件,同时在处理过程中看到屏幕上的错误。
Just for your convenience adding everything together with the formatter in one package:
My use case was to redirect stdout to a datafile while seeing errors on the screen during processing.
是的。您必须为日志记录定义多个处理程序。
http://docs.python.org/library/logging。 html#logging-to-multiple-destinations
http ://docs.python.org/library/logging.handlers.html#module-logging.handlers
Yes. You must define multiple handlers for your logging.
http://docs.python.org/library/logging.html#logging-to-multiple-destinations
http://docs.python.org/library/logging.handlers.html#module-logging.handlers
我遇到了同样的问题,并编写了一个名为 SplitStreamHandler 的自定义日志记录处理程序:
I had the same problem and wrote a custom logging handler called SplitStreamHandler:
从更新的文档来看,它现在很好地涵盖了这种情况。
http://docs.python.org/howto/logging.html#logging -advanced-tutorial
我在评论中提到了示例中需要进行的两项更改,以使输出转到标准输出。您还可以使用过滤器根据级别进行重定向。
了解更改的更多信息位于
http://docs.python.org/library/logging.handlers .html#module-logging.handlers
right from the updated docs, it cover this case pretty well now.
http://docs.python.org/howto/logging.html#logging-advanced-tutorial
i've mentioned on comments the two changes required from the example to make the output go to stdout. you may also use filters to redirect depending on the level.
more information to understand the changes is at
http://docs.python.org/library/logging.handlers.html#module-logging.handlers
这是我使用的紧凑解决方案(使用 Python 3.10 进行测试):
Here's the compact solution I use (tested with Python 3.10):
您必须将处理程序与您想要将记录的语句发送到的每个流关联。
Python 有 5 个日志记录级别 -
CRITICAL
、错误
、警告
、信息
和调试
。假设您希望关键和错误日志应转到
STDERROR
,而其他日志应转到STDOUT
- 如上表所示。以下是您应该遵循的步骤。
Step1 初始化日志模块
Step 2 创建两个流,一个附加到 STDERROR,
其他到 STDOUT
第 3 步 设置每个处理程序的日志级别。这告诉处理程序仅记录日志级别等于或高于我们设置的级别的语句。例如。如果我们将其设置为
WARNING
,则处理程序将仅处理WARNING
、ERROR
和CRITICAL
级别的日志记录语句。还要确保日志级别仅在处理程序中设置,而不是在log
对象中设置,这样就不会发生冲突。现在有一个问题。
hStErr
仅输出ERROR
和CRITICAL
的日志记录,而hStOut
将输出所有 5 个级别的日志记录。请记住,setLevel
仅告知应处理的最低日志记录级别,因此还将处理所有更高的级别。为了限制hStOut
不处理ERROR
和CRITICAL
,我们使用过滤器。第 4 步 指定过滤器,以便
hStOut
不会处理ERROR
和CRITICAL
第 5 步 将这些处理程序添加到记录器
以下是所有部分。
当我们运行这个脚本时输出。
Pycharm IDE 将 std 错误的输出颜色显示为红色。下图显示了上面的
错误日志
语句被发送到stderr
。如果我们注释上面脚本中的
addFilter
行,我们将看到以下输出。请注意,如果没有过滤器,
hStOut
将从 INFO 和 ERROR 中输出日志语句,而对于 INFOhStErr
不输出任何内容,并且hStOut
输出单个语句 -信息日志
You will have to associated a handler to each stream you want logged statements to be sent to.
Python has 5 logging levels -
CRITICAL
,ERROR
,WARNING
,INFO
, andDEBUG
.Let's assume you want that Critical and Error logs should go to
STDERROR
, while others should go toSTDOUT
- as shown in the table above.Here are the steps you should follow.
Step1 Initialize the logging module
Step 2 Create two streams, one attached to STDERROR,
other to STDOUT
Step 3 Set log level for each handler. This tells handler to only log statements which have log level equal to or higher than the level we set. eg. if we set this to
WARNING
, the handler will only handle logging statements forWARNING
,ERROR
, andCRITICAL
levels. Also make sure that the log level is set in handler only, and not with thelog
object, so that there is no conflictNow here comes a catch. While
hStErr
will only output logging forERROR
andCRITICAL
,hStOut
will output for all 5 levels. Remember thatsetLevel
only tells the minimum logging level which should be handled, so all levels which are greater will also be handled. To limithStOut
to not handleERROR
andCRITICAL
, we use a filter.Step 4 Specify a filter so that
ERROR
, andCRITICAL
aren't handled byhStOut
Step 5 Add these handlers to logger
Here are all the pieces together.
Output when we run this script.
Pycharm IDE colors output from std error red. The following image shows that the
error log
statement above was sent tostderr
.If we comment the
addFilter
line in above script, we will see the following output.Note that without filter
hStOut
will output logging statements from both INFO and ERROR, while for INFOhStErr
outputs nothing, andhStOut
outputs a single statement -info log
不一定是个好主意(看到信息和调试消息与正常输出混合在一起可能会令人困惑!),但可行,因为您可以拥有多个处理程序对象和自定义 过滤器 为每个处理程序选择要处理的日志记录。
Not necessarily a good idea (it could be confusing to see info and debug messages mixed in with normal output!), but feasible, since you can have multiple handler objects and a custom filter for each of them, in order to pick and choose which log records each handler gets to handle.