zoukankan      html  css  js  c++  java
  • python3 日志重复打印logger

    在python2中正常的日志,单只直接使用python3,发现日志重复了,其实是handlers多添加的原因,

    python2代码

    -----------------------------------------------------------------------------------------------------------------------------------------------------------

     def my_log(msg):

             if logging.getLogger('log.log'):

                    return logging.getLogger('log.log')

             logger = logging.getLogger('log.log')

             ch = logging.StreamHandler()

             ch.setLevel(logging.ERROR)

             fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

             ch.setFormatter(fmt)

             logger.addHandler(ch) logger.error(msg)


    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    适当修改一下:

    python3代码:
    -----------------------------------------------------------------------------------------------------------------------------------------------------------

     def my_log(msg):

             if logging.getLogger('log.log'):

                    return logging.getLogger('log.log')

             logger = logging.getLogger('log.log')

            if  not logger.handlers:

                 ch = logging.StreamHandler()

                 ch.setLevel(logging.ERROR)

                 fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

                 ch.setFormatter(fmt)

                 logger.addHandler(ch) logger.error(msg)
    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    加一个重复判断就行

     

  • 相关阅读:
    mysql索引
    mysql主从复制(同步)
    MySQL事务与锁
    四大高阶函数
    客户端、服务端通信值统计字符串个数【网络程序设计
    《Unicast QoS Routing Algorithms for SDN Survey 2018》【毕设
    CDQ分治【待补充,数据结构
    KD树学习小结【待补充,数据结构
    线段树模板【数据结构
    【牛客网】牛客练习赛19 F 算式子【数学--递推 、前缀、数字】
  • 原文地址:https://www.cnblogs.com/YouXiangLiThon/p/11833758.html
Copyright © 2011-2022 走看看