python – 有没有办法更改未使用basicConfig配置的记录器对象的filemode?

前端之家收集整理的这篇文章主要介绍了python – 有没有办法更改未使用basicConfig配置的记录器对象的filemode?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我使用logger = logging.getLogger(“Name”)创建一个logger对象,我无法将filemode从append(‘a’)更改为write(‘w’).我可以使用带有basicConfig的root记录器,但是当我想要的是从DEBUG级别开始的我自己的消息时,我会记录很多系统调试消息.

我希望(1)将我自己的记录器对象的filemode更改为’w’
或(2)向根记录器添加过滤器.甚至可以从根记录器中过滤掉这些调试消息吗?

def create_log():
    # create logger for "Sample App"

    # create logger for "Sample App"
    logger = logging.getLogger('automated_testing')
    logger.setLevel(logging.DEBUG)

    # create file handler which logs even debug messages
    fh = logging.FileHandler('results.log')
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler(stream=sys.stdout)
    ch.setLevel(logging.DEBUG)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('[%(asctime)s] %(levelname)8s --- %(message)s ' +
                                  '(%(filename)s:%(lineno)s)',datefmt='%Y-%m-%d %H:%M:%S')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(ch)
    logger.addHandler(fh)

    return logger

解决方法

就像是:
import sys
import logging

def create_logger():
    # create logger for "Sample App"
    logger = logging.getLogger('automated_testing')
    logger.setLevel(logging.DEBUG)

    # create file handler which logs even debug messages
    fh = logging.FileHandler('results.log',mode='w')
    fh.setLevel(logging.DEBUG)

    # create console handler with a higher log level
    ch = logging.StreamHandler(stream=sys.stdout)
    ch.setLevel(logging.INFO)

    # create formatter and add it to the handlers
    formatter = logging.Formatter('[%(asctime)s] %(levelname)8s --- %(message)s ' +
                                  '(%(filename)s:%(lineno)s)',datefmt='%Y-%m-%d %H:%M:%S')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    # add the handlers to the logger
    logger.addHandler(ch)
    logger.addHandler(fh)

    return logger

logger = create_logger()


logger.log(logging.NOTSET,"NOTSET   Message - 0")
logger.log(logging.DEBUG,"DEBUG    Message - 10")
logger.log(logging.INFO,"INFO     Message - 20")
logger.log(logging.WARNING,"WARNING  Message - 30")
logger.log(logging.CRITICAL,"CRITICAL Message - 40")

打印到stdout:

[2015-03-16 17:51:08]     INFO --- INFO     Message - 20 (temp3.py:34)
[2015-03-16 17:51:08]  WARNING --- WARNING  Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)

写入(不附加)到results.log:

[2015-03-16 17:51:08]    DEBUG --- DEBUG    Message - 10 (temp3.py:33)
[2015-03-16 17:51:08]     INFO --- INFO     Message - 20 (temp3.py:34)
[2015-03-16 17:51:08]  WARNING --- WARNING  Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)

DEBUG记录在results.txt中,而只有INFO被发送到stdout.

请注意,NOTSET日志条目随后会传递给根记录器,因为您在根记录器上没有任何处理程序,因此被丢弃.

原文链接:https://www.f2er.com/python/241843.html

猜你在找的Python相关文章