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

    加一个重复判断就行

     

  • 相关阅读:
    centos 安装 Lamp(Linux + Apache + PHP) 并安装 phpmyadmin
    mysql常用内置函数-查询语句中不能使用strtotime()函数!
    Windows下 wamp下Apache配置虚拟域名
    thinkphp ajax调用demo
    phpMailer 手册
    wampServer2.2 You don't have permission to access /phpmyadmin/ on this server.
    打印对象
    最全的CSS浏览器兼容问题
    html 视频播放器
    C语言入门-结构类型
  • 原文地址:https://www.cnblogs.com/YouXiangLiThon/p/11833758.html
Copyright © 2011-2022 走看看