项目中封装了logging库为log.py,实现既把日志输出到控制台, 又写入日志文件文件。
环境:python3.7.3
项目中,多个文件共用logger,出现重复打印问题,解决流程记录如下:
文件和调用方式如下:
log.py v1
#encoding = utf-8
###
# @ Description: 日志封装文件
# @ Author: fatih
# @ Date: 2020-12-30 10:48:00
# @ FilePath: \mechineInfo\utils\log.py
# @ LastEditors: fatih
# @ LastEditTime: 2021-01-11 16:18:30
###
import logging
# 既把日志输出到控制台, 还要写入日志文件
class Logger():
def __init__(self, logname="info", loglevel=logging.DEBUG, loggername=None):
'''
指定保存日志的文件路径,日志级别,以及调用文件
将日志存入到指定的文件中
'''
# 创建一个logger
self.logger = logging.getLogger(loggername)
self.logger.setLevel(loglevel)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(logname)
fh.setLevel(loglevel)
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(loglevel)
# 定义handler的输出格式
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('[%(levelname)s]%(asctime)s %(filename)s:%(lineno)d: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
self.logger.addHandler(fh)
self.logger.addHandler(ch)
#测试日志,正式环境可删除
self.logger.fatal("set logger")
def getlog(self):
self.logger.fatal("get logger")
return self.logger
问题一:多文件调用共用logger,重复打印
调用方式:
a.py v1
#!/usr/bin/python
# -*- coding:utf-8 -*-
import logging
from log import Logger
logger = Logger().getlog()
logger.debug("this is a")
b.py v1
#!/usr/bin/python
# -*- coding:utf-8 -*-
import logging
from log import Logger
import a
logger = Logger().getlog()
logger.debug("this is b")
此时执行b.py的结果如下:
$ python3 b.py
[CRITICAL]2021-01-20 10:51:17,966 log.py:44: set logger
[CRITICAL]2021-01-20 10:51:17,966 log.py:47: get logger
[DEBUG]2021-01-20 10:51:17,967 a.py:7: this is a
[CRITICAL]2021-01-20 10:51:17,968 log.py:44: set logger
[CRITICAL]2021-01-20 10:51:17,968 log.py:44: set logger
[CRITICAL]2021-01-20 10:51:17,968 log.py:47: get logger
[CRITICAL]2021-01-20 10:51:17,968 log.py:47: get logger
[DEBUG]2021-01-20 10:51:17,969 b.py:8: this is b
[DEBUG]2021-01-20 10:51:17,969 b.py:8: this is b
从结果可以看出来,存在logger共用,导致重复打印。
原因分析
调用方式logger = Logger().getlog(),即self.logger = logging.getLogger(logger)
logger参数并未传递,所以得到的self.logger是RootLogger
而RootLogger是一个python程序内全局唯一的,所有Logger对象的祖先。
也就是说先打开的文件中对log的设置,后打开的文件都会受到影响,都会走一遍logger的继承关系。
b.py import a, 先执行a, 然后调用getLogger,得到RootLogger,打印一次,再执行一次在a.py中打开的RootLogger,再打印一次
解决方案
不同文件调用,使用不同的loggername
a.py v2
logger = Logger("a.log", logging.DEBUG, __name__).getlog()
logger.debug("this is a")
b.py v2
logger = Logger("b.log", logging.DEBUG, __name__).getlog()
logger.debug("this is b")
此时重新执行
命令行显示正常了
[CRITICAL]2021-01-20 11:02:22,763 log.py:44: set logger
[CRITICAL]2021-01-20 11:02:22,764 log.py:47: get logger
[DEBUG]2021-01-20 11:02:22,764 a.py:7: this is a
[CRITICAL]2021-01-20 11:02:22,765 log.py:44: set logger
[CRITICAL]2021-01-20 11:02:22,765 log.py:47: get logger
[DEBUG]2021-01-20 11:02:22,765 b.py:8: this is
问题二:单文件重复调用logger,重复打印
a.py v2
logger = Logger("a.log", logging.DEBUG, __name__).getlog()
logger.debug("this is a")
logger = Logger("a.log", logging.DEBUG, __name__).getlog()
logger.debug("this is a 2")
打印结果:
[CRITICAL]2021-01-20 11:39:04,312 log.py:44: set logger
[CRITICAL]2021-01-20 11:39:04,312 log.py:47: get logger
[DEBUG]2021-01-20 11:39:04,312 a.py:7: this is a
[CRITICAL]2021-01-20 11:39:04,313 log.py:44: set logger
[CRITICAL]2021-01-20 11:39:04,313 log.py:44: set logger
[CRITICAL]2021-01-20 11:39:04,313 log.py:47: get logger
[CRITICAL]2021-01-20 11:39:04,313 log.py:47: get logger
[DEBUG]2021-01-20 11:39:04,313 a.py:10: this is a 2
[DEBUG]2021-01-20 11:39:04,313 a.py:10: this is a 2
此时例如执行logger = Logger("a.log", logging.DEBUG, name).getlog(),并且后续日志对象的第三个传参都相同的时候,
此后多次打印日志会出现日志信息条数线性增加
例如第一次打印一条,第二条打印相同的两条日志,第三次打印相同的三条日志…
原因解析
因为logger的name被固定,
所以当你第一次为logger对象添加FileHandler对象之后,
如果没有移除上一次的FileHandler对象,第二次logger对象就会再次获得相同的FileHandler对象,即拥有两个FileHandler对象,
最终造成打印两次,
同样,如果此时没有立即移除上一次的FileHandler对象,第三次logger对象就会再次获得相同的FileHandler对象,即拥有三个FileHandler象,最终打印3次…
解决方案
- 每次添加日志,创建与上次日志对象的loggername属性不同的logger对象
- 调用会导致同时存在超多logger对象
- 不推荐
- 每次logger输出之后,移除FileHandler对象
- 重写输出接口,输出后增加removeHandler操作
- 繁琐,复杂,不推荐
- 通过判断logger对象的handlers属性,或者hasHandlers函数,保持同一loggername对应的FileHander唯一
- 只需要增加一行代码,if not logger.handlers: 或if not self.logger.hasHandlers()只有不存在Handler时才设置Handler
- 简洁,推荐
第三种方案,改写log.py v1代码如下
log.py v2
#encoding = utf-8
###
# @ Description: 日志封装文件
# @ Author: fatih
# @ Date: 2020-12-30 10:48:00
# @ FilePath: \mechineInfo\utils\log.py
# @ LastEditors: fatih
# @ LastEditTime: 2021-01-11 16:18:30
###
import logging
# 既把日志输出到控制台, 还要写入日志文件
class Logger():
def __init__(self, logname="info", loglevel=logging.DEBUG, loggername=None):
'''
指定保存日志的文件路径,日志级别,以及调用文件
将日志存入到指定的文件中
'''
# 创建一个logger
self.logger = logging.getLogger(loggername)
self.logger.setLevel(loglevel)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(logname)
fh.setLevel(loglevel)
if not self.logger.handlers:
#或者使用如下语句判断
#if not self.logger.hasHandlers():
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(loglevel)
# 定义handler的输出格式
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('[%(levelname)s]%(asctime)s %(filename)s:%(lineno)d: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 给logger添加handler
self.logger.addHandler(fh)
self.logger.addHandler(ch)
self.logger.fatal("add handler")
self.logger.fatal("set logger")
def getlog(self):
self.logger.fatal("get logger")
return self.logger
此时调用a.py v2
结果如下:
[CRITICAL]2021-01-20 11:55:08,427 log.py:44: add handler
[CRITICAL]2021-01-20 11:55:08,427 log.py:46: set logger
[CRITICAL]2021-01-20 11:55:08,427 log.py:49: get logger
[DEBUG]2021-01-20 11:55:08,427 a.py:7: this is a
[CRITICAL]2021-01-20 11:55:08,428 log.py:46: set logger
[CRITICAL]2021-01-20 11:55:08,428 log.py:49: get logger
[DEBUG]2021-01-20 11:55:08,429 a.py:10: this is a 2
至此,问题解决
参考:
python的logging模块浅析
来源:https://blog.csdn.net/u011417820/article/details/112861970
标签:01,20,log,Python,py,2021,共用,logger From: https://www.cnblogs.com/liuyanhang/p/17281146.html