我从只有一个处理程序的记录器中获得了重复的日志输出。
我该如何调试呢?
代码(应可复制粘贴):
import io
import logging
import os
import sys
from typing import Optional
DEFAULT_LOG_LEVEL = logging.INFO
def get_sub_loggers():
return [
logging.getLogger('a'),
logging.getLogger('b'),
logging.getLogger('c')
]
def safe_add_handler(logger: logging.Logger, h: logging.Handler) -> None:
if h not in logger.handlers:
logger.addHandler(h)
def safe_add_filter(logger: logging.Logger, f: logging.Filter) -> None:
if f not in logger.filters:
logger.addFilter(f)
def configure_logging(main_logger: logging.Logger,
log_level = DEFAULT_LOG_LEVEL,
log_file = None,
fmt: Optional[str] = None,
force_add_handler: bool = False) -> None:
main_logger.setLevel(log_level)
logging.captureWarnings(True) # send all warnings to be logged
if force_add_handler or not main_logger.hasHandlers():
if log_file is None or log_file == '-':
log_file = sys.stderr
if isinstance(log_file, (str, os.PathLike)):
handler = logging.FileHandler(log_file)
else:
handler = logging.StreamHandler(log_file)
handler.setFormatter(logging.Formatter(fmt if fmt is not None else logging.BASIC_FORMAT))
main_logger.addHandler(handler)
log_output_location = handler.stream.name if isinstance(handler, logging.StreamHandler) else handler.baseFilename
main_logger.info('Writing logs to {}'.format(log_output_location))
else:
main_logger.info('Using existing log handlers')
main_logger.info('Using log level {}'.format(log_level))
for logger in get_sub_loggers():
logger.setLevel(log_level)
for h in main_logger.handlers:
safe_add_handler(logger, h)
for f in main_logger.filters:
safe_add_filter(logger, f)
main_logger.debug('Configured logger {}'.format(logger.name))
main_logger.info('Configured submodule loggers.')
configure_logging(logging.getLogger())
a_logger = logging.getLogger('a')
assert len(a_logger.handlers) == 1
# Should only output "hi" once, but it does it twice
a_logger.info('hi')
答案 0 :(得分:1)
记录器propagate默认将事件记录到其祖先的处理程序中。 a_logger
可能只有一个处理程序,但是其父级(即根记录程序)也有一个处理程序(实际上是相同的处理程序)。 a_logger.info('hi')
由两个处理程序(实际上是同一处理程序两次)处理。
您不需要将相同的处理程序附加到每个记录器。只需将其附加到根记录器即可。