zoukankan      html  css  js  c++  java
  • python logger 动态设置日志名

    代码:

    import logging
    
    logger = logging.getLogger('')
    logger.setLevel(level=logging.INFO)
    
    def setLogName(name):
        handler = logging.FileHandler(name, mode='w')
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', '%a, %d %b %Y %H:%M:%S')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    
    if __name__ == "__main__":
        setLogName('/Users/test/test_log.txt')
        logger.info("start func111")
        logger.info("start func222")
        logger.info("start func333")

    结果:

    cat test_log.txt 

    Tue, 12 Mar 2019 18:44:53 log.py[line:32] INFO start func111

    Tue, 12 Mar 2019 18:44:53 log.py[line:33] INFO start func222

    Tue, 12 Mar 2019 18:44:53 log.py[line:34] INFO start func333

  • 相关阅读:
    A
    B
    C
    I
    公共最大字串长度
    docker run 的背后的故事(zz)
    python之多并发socket(zz)
    Python垃圾回收机制:gc模块(zz)
    我要做的git的分享(zz)
    SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)
  • 原文地址:https://www.cnblogs.com/mianbaoshu/p/10518726.html
Copyright © 2011-2022 走看看