python日志记录处理程序中setLevel的意义是什么? [英] What is the point of setLevel in a python logging handler?

查看:125
本文介绍了python日志记录处理程序中setLevel的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下代码:

import logging
import logging.handlers

a = logging.getLogger('myapp')
h = logging.handlers.RotatingFileHandler('foo.log')
h.setLevel(logging.DEBUG)
a.addHandler(h)

# The effective log level is still logging.WARN
print a.getEffectiveLevel() 
a.debug('foo message')
a.warn('warning message')

我希望在处理程序上设置logging.DEBUG会导致将调试级别的消息写入日志文件.但是,这会打印出30的有效级别(等于logging.WARNING,默认值),并且仅将warn消息记录到日志文件中,而不记录调试消息.

I expect that setting logging.DEBUG on the handler would cause debug-level messages to be written to the log file. However, this prints 30 for the effective level (equal to logging.WARNING, the default), and only logs the warn message to the log file, not the debug message.

看来,处理程序的日志级别正在下降在地板上,例如它被默默地忽略了.这让我感到奇怪,为什么在处理程序上根本没有setLevel?

It appears that the handler's log level is being dropped on the floor, e.g. it's silently ignored. Which makes me wonder, why have setLevel on the handler at all?

推荐答案

它允许进行更好的控制.默认情况下,root记录程序已设置WARNING级别,这意味着它不会打印较低级别的消息(无论如何设置处理程序的级别!).但是,如果您将根记录程序的级别设置为DEBUG,则确实会将消息发送到日志文件:

It allows finer control. By default the root logger has WARNING level set, this means that it wont print messages with lower level(no matter how the handlers' levels are set!). But, if you set the root logger's level to DEBUG, indeed the message get sent to the log file:

import logging
import logging.handlers

a = logging.getLogger('myapp')
a.setLevel(logging.DEBUG)   # set root's level
h = logging.handlers.RotatingFileHandler('foo.log')
h.setLevel(logging.DEBUG)
a.addHandler(h)
print a.getEffectiveLevel() 
a.debug('foo message')
a.warn('warning message')

现在,要添加不记录调试信息的新处理程序的映像. 您可以通过简单地设置处理程序日志记录级别来做到这一点:

Now, image that you want to add a new handler that doesn't record debug information. You can do this by simply setting the handler logging level:

import logging
import logging.handlers

a = logging.getLogger('myapp')
a.setLevel(logging.DEBUG)   # set root's level

h = logging.handlers.RotatingFileHandler('foo.log')
h.setLevel(logging.DEBUG)
a.addHandler(h)

h2 = logging.handlers.RotatingFileHandler('foo2.log')
h2.setLevel(logging.WARNING)
a.addHandler(h2)

print a.getEffectiveLevel() 
a.debug('foo message')
a.warn('warning message')

现在,日志文件foo.log将包含这两个消息,而文件foo2.log将仅包含警告消息.您可能对只包含错误级别消息的日志文件感兴趣,然后只需添加Handler并将其级别设置为logging.ERROR,所有内容都使用相同的Logger.

Now, the log file foo.log will contain both messages, while the file foo2.log will only contain the warning message. You could be interested in having a log file of only error-level messages, then simply add a Handler and set its level to logging.ERROR, everything using the same Logger.

您可能会将Logger日志记录级别视为对给定记录器及其处理程序哪些消息感兴趣"的全局限制.记录器之后考虑的消息将发送到处理程序,这些处理程序将执行其自己的筛选和记录过程.

You may think of the Logger logging level as a global restriction on which messages are "interesting" for a given logger and its handlers. The messages that are considered by the logger afterwards get sent to the handlers, which perform their own filtering and logging process.

这篇关于python日志记录处理程序中setLevel的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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