本文介绍了关于python日志记录中的NOTSET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如 logger.setLevel 文件所说:

所以我认为,如果我创建一个具有NOTSET级别的root记录器,则将显示调试和信息日志.

so I think if I create a root logger, with level NOTSET, the debug and info log will display.

代码使用basicConfig将根记录程序的级别设置为NOTSET是正确的:

The code use basicConfig to set root logger's level to NOTSET is right:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

logging.basicConfig(level=logging.NOTSET)
log = logging.getLogger()

log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')

输出为:

DEBUG:root:debug
INFO:root:info
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical

但是,如果我创建一个根记录器,并向其添加具有NOTSET级别的处理程序,例如:

But if I create a root logger, and add handler with NOTSET level to it, such as:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

log = logging.getLogger()
hd = logging.StreamHandler()
hd.setLevel(logging.NOTSET)
log.addHandler(hd)

log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')

输出为:

warning
error
critical

但是我认为它也会输出调试和信息消息.

but I think it will also output the debug and info message.

推荐答案

好,我在文档中误解了 Logger的级别 Handler的级别:

OK, I misunderstand the Logger's level and Handler's Level, in the doc:

如果我将代码更改为此,可以:

If I change code to this, will be ok:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging

log = logging.getLogger()
log.setLevel(logging.NOTSET) # Set Logger's level to NOTSET, default is WARNING
#print "Logger's Level: ", log.level
hd = logging.StreamHandler()
hd.setLevel(logging.NOTSET)
#print "Handler's Level: ", hd.level
log.addHandler(hd)

log.debug('debug')
log.info('info')
log.warning('warning')
log.error('error')
log.critical('critical')

这篇关于关于python日志记录中的NOTSET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:19