带有FileHandler的Python logging.get_Logger(name)不会写入文件 [英] Python logging.get_Logger(name) with FileHandler does not write to file

查看:116
本文介绍了带有FileHandler的Python logging.get_Logger(name)不会写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我给记录器取一个名称并添加FileHandler,它将不会写入文件.

If I get the logger with a name and add a FileHandler it does not write to the file.

这可以正常工作并将其正确写入文件:

This works and writes correctly to the file:

log = logging.getLogger()
fh = logging.FileHandler(logfile)
log.addHandler(fh)
fh_fmt = logging.Formatter("%(asctime)s (%(levelname)s)\t: %(message)s")
fh.setFormatter(fh_fmt) 
log.setLevel(logging.INFO)

这不会写入文件:

log = logging.getLogger(name)
fh = logging.FileHandler(logfile)
log.addHandler(fh)
fh_fmt = logging.Formatter("%(asctime)s (%(levelname)s)\t: %(message)s")
fh.setFormatter(fh_fmt) 
log.setLevel(logging.INFO)

唯一的区别是我得到了一个命名"记录器.

The only difference is that I get a 'named' logger.

推荐答案

这是一个相当古老的问题,但我相信我找到了潜在的问题和解决方案,至少在更新版本的 Python 中是这样.

This is a rather old question, but I believe I found the underlying problem and solution, at least with a newer version of Python.

第二个代码示例以 log = logging.getLogger(name)开头,其中 name 假定为代表记录器名称的字符串.由于提供了名称,因此该 log 成为根记录器.根据 Logger.setLevel(level)docs 适用于Python 3.6 +,

The second code example starts with log = logging.getLogger(name), where name is presumed to be a string representing the name of the logger. Since a name is provided, this log will not be the root logger. According to Logger.setLevel(level) docs for Python 3.6+,

创建记录器时,级别设置为 NOTSET (如果该记录器是根记录器,则将处理所有消息;如果该记录器不是根记录器,则将委派给父级)根记录器).

When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger).

这告诉我们,我们必须设置记录器的级别,以便它实际上将处理消息,而不是将其传递给根记录器.

This tells us that we have to set the level of our logger so that it will actually process the messages instead of passing it to the root logger.

这是我在Python 3.7中编写的代码示例,该代码示例无法正常工作:

This is a code example I wrote (in Python 3.7) that does not work:

from pathlib import Path
import logging

formatter = logging.Formatter('%(name)s [%(levelname)s] %(message)s')
log_file_dir = Path('./log/')
config_file = 'config_file.txt'
config_file_path = log_file_dir / config_file

logger = logging.getLogger('example_logger')
fh = logging.FileHandler(config_file_path, mode='w')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Start Configuration Log')

这一行通过添加一行来实现:

And this one works by adding one line:

from pathlib import Path
import logging

formatter = logging.Formatter('%(name)s [%(levelname)s] %(message)s')
log_file_dir = Path('./log/')
config_file = 'config_file_2.txt'
config_file_path = log_file_dir / config_file

logger = logging.getLogger('example_logger')
logger.setLevel(logging.INFO) # <------ Or the applicable level for your use-case
fh = logging.FileHandler(config_file_path, mode='w')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('Start Configuration Log')

注意:第一个代码示例确实创建了所选的日志文件,但不会将'Start Configuration Log'写入文件.

Note: The first code example does create the chosen log file, but does not write 'Start Configuration Log' to the file.

这篇关于带有FileHandler的Python logging.get_Logger(name)不会写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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