python logging 日志重复打印两次

logging日志重复打印问题

问题描述

在查看项目文件输出的运行日志时发现每句日志都会打印两次(info和debug均出现此问题)

解决

工程文件中一般会配置log,解决方法是将参数propagate修改为False

# -*- coding:utf-8 -*-
"""log_util.py
~~~~~~~~~~~~~~
提供日志支持。:copyrith: (c) 2020 Dingdang Cat
:modified: 2020-9-28
"""
import os
import logging
from logging.config import dictConfigdef configure_logger(name, log_path):"""配置logger提供两种logger(name): debug可记录debug日志,info可记录info日志。注意本程序会根据传入的`log_path`生成两个日志文件,一个是`ERR`开头的日志,记录错误日志;另一个是`INFO`或者`DEBUG`开头,内容自明。Args:name (:str): logger handler名称,`info`或者`debug`之一。log_path (:str): 日志位置,绝对路径"""try:log_dir, log_file = os.path.split(log_path)except Exception as e:raise ValueError("log_path error, MUST be absolute path")dictConfig({'version': 1,'disable_existing_loggers': False,'formatters': {'default': {'format': '%(asctime)s - %(module)s[:%(lineno)d] - %(levelname)s - %(message)s','datefmt': '%Y-%m-%d %H:%M:%S'}},'handlers': {'console_handler': {'level': logging.DEBUG,'class': 'logging.StreamHandler','formatter': 'default','stream': 'ext://sys.stdout'},'info_handler': {'level': logging.INFO,'class': 'logging.handlers.TimedRotatingFileHandler','formatter': 'default','when': 'D','interval': 1,'filename': os.path.join(log_dir, log_file),'backupCount': 7,"encoding": "utf8"},'debug_handler': {'level': logging.DEBUG,'class': 'logging.handlers.TimedRotatingFileHandler','formatter': 'default','when': 'D','interval': 1,'filename': os.path.join(log_dir, log_file),'backupCount': 7,"encoding": "utf8"},"error_handler": {"class": "logging.handlers.RotatingFileHandler","level": logging.ERROR,"formatter": "default","filename": os.path.join(log_dir, log_file),"maxBytes": 10485760,"backupCount": 7,"encoding": "utf8"}},'loggers': {'': {  # root logger'level': logging.DEBUG,'handlers': ['console_handler', 'debug_handler', 'error_handler'],},'debug': {'level': logging.DEBUG,'handlers': ['console_handler', 'debug_handler', 'error_handler'],'propagate': False},'info': {'level': logging.INFO,'handlers': ['console_handler', 'info_handler', 'error_handler'],'propagate': False}}})return logging.getLogger(name)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部