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

    加一个重复判断就行

     

  • 相关阅读:
    页面反向映射之文件页面
    页面反向映射之匿名页面
    cp so文件导致进程SIGBUS或者SEGV原因分析
    由systemtap直接修改内核代码段想到的
    epoll的内核实现
    从一些现象看printf的缓冲机制
    Linux由浅入深学习 (转)
    每天一个Linux命令 (转)
    Redis与数据库同步问题
    PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
  • 原文地址:https://www.cnblogs.com/YouXiangLiThon/p/11833758.html
Copyright © 2011-2022 走看看