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)
    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    加一个重复判断就行

     

  • 相关阅读:
    linux下聊天工具的安装
    Linux上OpenLDAP集群
    Linux下python基础调试
    曾仕强主讲:易经的奥秘(全文讲义)
    Linux单网卡多个IP(或者多个网卡多个IP)设置
    单播、广播、组播的区别和特点
    谷歌招聘 变态15题你会做几道?
    Gartner再评RSA为网络欺诈检测领导者 狼人:
    云安全 安全领域的最大热点之一 狼人:
    金山毒霸专业版高调上线 宣称杀毒速度增3倍 狼人:
  • 原文地址:https://www.cnblogs.com/YouXiangLiThon/p/11833758.html
Copyright © 2011-2022 走看看