使用 FileHandler 的 Pythonlogging.get_Logger(name) 不会写入文件

发布于 2024-12-11 13:18:22 字数 578 浏览 0 评论 0原文

如果我获取带有名称的记录器并添加 FileHandler,它不会写入文件。

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

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)

唯一的区别是我得到了一个“命名”记录器。

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)

This does not write to the file:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-12-18 13:18:22

这是一个相当老的问题,但我相信我找到了潜在的问题和解决方案,至少使用较新版本的 Python 是这样。

第二个代码示例以 log =logging.getLogger(name) 开头,其中 name 被假定为表示记录器名称的字符串。由于提供了名称,因此该日志不是是根记录器。根据 Logger.setLevel(level) 文档< /a> 对于 Python 3.6+,

创建记录器时,级别设置为NOTSET(这会导致当记录器是根记录器时处理所有消息,或者当记录器是非记录器时将委托给父记录器)根记录器)。

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

这是我编写的代码示例(在 Python 3.7 中)不起作用:

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')

并且这个代码示例通过添加一行来工作:

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 ' 到文件中。

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

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+,

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.

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')

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文