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

    加一个重复判断就行

     

  • 相关阅读:
    (C/C++学习)6.数组指针和指针数组
    (C/C++学习)5.C++中的虚继承-虚函数-多态解析
    (C/C++学习)4.C++类中的虚函数表Virtual Table
    (C/C++学习)3.C++中cin的成员函数(cin.get();cin.getine()……)
    (C/C++学习)2.C语言中文件流操作基本函数总结
    关于for,while与do while
    计算机算法-C语言-统计字母数字个数解
    计算书费
    Truncate table
    sqlserver 在脚本中,为所有字符前没有N标记的字符增加N
  • 原文地址:https://www.cnblogs.com/YouXiangLiThon/p/11833758.html
Copyright © 2011-2022 走看看