python logging logger with only filehandler is writing to both file and stdout/err

huangapple go评论78阅读模式
英文:

python logging logger with only filehandler is writing to both file and stdout/err

问题

您遇到的问题是在使用Python的logging模块时,日志除了被写入到文件中之外,还会输出到标准输出(stdout)和标准错误(stderr)。您想要解决这个问题。

这个问题可能是因为默认情况下,logging模块会在根记录器(root logger)上创建一个默认的处理程序,将日志消息发送到标准输出(stdout)。要解决这个问题,您可以在创建自定义记录器(logger)之前禁用根记录器的默认处理程序。

以下是一种可能的解决方法:

import logging

# 禁用根记录器的默认处理程序
logging.getLogger().handlers = []

# 创建自定义记录器
logger = logging.getLogger('custom_logger')

# 此时您的自定义记录器将不再输出到标准输出(stdout)
logger.handlers = [logging.FileHandler('/tmp/log_2023-06-29_15-29-25.log')]

# 现在,只有文件处理程序会接收日志消息
logger.info('test')

这样,您就可以确保日志只会被写入文件而不会输出到标准输出(stdout)和标准错误(stderr)。

英文:

I have a logger that only has a file handlers, which works properly. However for some reason it will also always output to stdout/err too.

In [10]: import logging

In [11]: logger = logging.getLogger('custom_logger')

In [12]: logger.handlers
Out[12]: [<FileHandler /tmp/log_2023-06-29_15-29-25.log (NOTSET)>]

In [13]: logger.info('test')
INFO:custom_logger:test   <<< This shouldn't happen!

The FileHandler is a simple handler with a custom formatter (json formatter), but the same issue also happens when using no formatter (default formatter): the logs get written to both the File and the standard (error) output / terminal.

using .clear() on the handlers before adding the File handler didn't help. And as you can see in .handlers only the FileHandler is listed. What could be happening here? (The example I provided is with ipython, but it is not a matter of ipython either as the same issue happens everywhere: with python3 direct use, when running tests etc...)

Am I missing something obvious? python3.10.6

EDIT:

Even when clearing all handlers, it will still log to stdout/err (but not to file)

In [3]: logger.handlers.clear()

In [4]: logger.info('test')
INFO:custom_logger:test

instead of not logging at all

答案1

得分: 0

找不到问题的来源。在代码的其他地方可能有其他日志记录器,所以它们可能以某种不好的方式相互作用。

我找到了以下的解决方法:

logger.propagate = False
logger.info('test')  <<< 不再显示任何内容

似乎它总是默认使用另一个日志记录器,即使日志记录器的级别(和FileHandler的级别)已经正确设置。我还尝试更改日志级别或根日志记录器(用于:logger.rootlogger.manager.rootlogger.parent),但这并没有起作用。

英文:

Couldn't find the source of the issue. There are other loggers in other places of the code so maybe they interact in a bad way somehow.

I found the following workaround:

logger.propagate = False
logger.info(&#39;test&#39;)  &lt;&lt;&lt; doesn&#39;t display anything anymore

It seems that it always ends up defaulting to an other logger even though the level is properly set for the logger (and the FileHandler). I also tried changing the log level or root logger (used for: logger.root, logger.manager.root and logger.parent) but this did not work.

huangapple
  • 本文由 发表于 2023年6月30日 03:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定