[英]Logging - changing level from INFO to DEBUG disables all logging messages
我最近才开始使用 Python 日志记录模块。 我已经阅读了文档和一些指南,但仍然对为什么日志级别的工作方式感到困惑。
我在脚本的开头定义了一个记录器:
import logging
log_file = f'test.log'
logger = logging.getLogger(__name__)
f_handler = logging.FileHandler(log_file)
f_handler.setLevel(logging.INFO)
f_format = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %H:%M:%S')
f_handler.setFormatter(f_format)
logger.addHandler(f_handler)
logger.info('Initialising.')
然后在整个代码中,我多次调用这个记录器 - 一些info
,一些exceptions
和warnings
。
然而,由于当前设置的某种原因,我只收到一条warning
消息(如果有),尽管我认为我应该收到高于并包括INFO
级别的所有消息。
为了尝试和测试它,我将记录器切换到DEBUG
但结果我得到了一个空日志。
至少我希望在日志中看到以下info
消息:
self.logger.info('Initialising.') #Right in the __init__ of the class
self.logger.info(f'Using {self.export_file} for export data.') #This one should always be printing unless script fails at the very start
self.logger.info(f'Using {self.model_file} model to predict values.') #Same with this one
self.logger.info('Encoding into a model completed successfully.') #This one as well if script completes successfully, which it normally does
我一定是做错了什么,但我不确定到底是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.