一种logging封装方法,不会产生重复log

在调试logging的封装的时候,发现已经调用了logging封装的函数,在被其它函数再调用时,会出现重复的logging。原因是不同的地方创建了不同的handler,所以会重复,可以使用暴力方法解决

暴力方式就是每次创建新的对象就清空logger.handlers

我常用的封装如下

import logging
import time,os
'''
    使用方法:
    import mylog
    log = mylog.Log().getlog()
    log.debug("###")
'''
class Log():

    def __init__(self,logger="mylog"):
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)
        self.log_time = "\\"+time.strftime("%Y-%m-%d_%H_%M", time.localtime())+".log"
        # 在进程路径创建log文件夹
        # self.log_path = os.path.join(os.getcwd() + "\\log")
        # 固定在mylog上一级创建
        self.log_path = os.path.join(os.path.dirname(os.path.dirname(__file__)) + "\\log")
        if os.path.exists(self.log_path) and os.path.isdir(self.log_path):
            pass
        else:
            os.makedirs(self.log_path)
        self.log_name = os.path.join(self.log_path + self.log_time)

        #因为多出调用logger会生成多个handlers,所以每次调用清空handler
        self.logger.handlers = [] 
        fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8')
        formatter = logging.Formatter('[%(levelname)s][%(asctime)s] [%(filename)s]->[%(funcName)s] line:%(lineno)d ---> %(message)s')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)
        
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        self.logger.addHandler(ch)
        
        fh.close()


    def getlog(self):
        return self.logger

if __name__ == "__main__":
    log = Log().getlog()
    log.debug("hello")

 

转载于:https://www.cnblogs.com/fengf233/p/10907343.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值