python - 有没有办法更改未使用 basicConfig 配置的记录器对象的文件模式?

标签 python logging filter

如果我使用 logger = logging.getLogger("Name") 创建记录器对象,我无法将文件模式从 append('a') 更改为 write ('w')。如果我将根记录器与 basicConfig 一起使用,我可以,但是当我想要的只是从 DEBUG 级别开始的我自己的消息时,我会记录很多系统调试消息。

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

def create_log():
    # 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")

打印到标准输出:

[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 日志条目会向上传递到根记录器,因为您在根记录器上没有任何处理程序,因此被丢弃。

关于python - 有没有办法更改未使用 basicConfig 配置的记录器对象的文件模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087297/

相关文章:

android - 通话记录中的自定义操作

c# - Microsoft Access 中的 bool 列和使用 linq 过滤数据

java - 在Java中处理多个过滤器-MySql

python - 如何从包含子项的内容中获取 Tree Widget 的大小?

python - 循环 Pandas 对象列表表现出奇怪的行为

python - Numpy:ImportError:无法导入名称 TestCase

C++高效大文件解析器

python - 为什么每次我播放 "play"音乐时 python 都会发出奇怪的声音?

java - 如何将静态日志文件包含到 Android 应用程序中,该文件在关闭应用程序时不会被删除?

php - 使用 PHP filter_input_array 过滤多维 POST