禁用根记录器的输出

禁用根记录器的输出

本文介绍了禁用根记录器的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为"logger.py"的文件中包含以下代码

I have the following code in a file called 'logger.py'

import logging

format = '%(asctime)s - %(name)-30s - %(levelname)-8s - %(message)s'
logging.basicConfig(level=logging.DEBUG,
                    format=format)

formatter = logging.Formatter(format)

fh = logging.FileHandler('test.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)

logging.getLogger().addHandler(fh)
logging.getLogger().addHandler(ch)

以及在另一个文件中

import logging
import logger

logger = logging.getLogger(__name__)
logger.info('Staring Scheduler')

并且我得到以下控制台输出

and I am getting the following console output

2014-07-14 22:27:10,915 - __main__                       - INFO     - Staring Scheduler
2014-07-14 22:27:10,915 - __main__                       - INFO     - Staring Scheduler

我无法禁用双重输出.我想使用额外的流处理程序来自定义打印到控制台的日志级别.将来,我还想使用RotatingFileHandler而不是简单的文件处理程序.

I am unable to disable the double output. I would like to use the extra streamhandler to customize the log level printed to the console. In the future I would also like to use the RotatingFileHandler instead of the simple file handler.

有人知道如何在保持第二个文件中的简单记录器设置的同时实现此目的吗?我已经搜索了,但是似乎没有解决办法.

Does anybody know how to achieve this while keeping the simple logger setup as in the second file? I have search, but no solution seems to do it.

更新1(已解决)

文件logger.py

import logging

logging.getLogger().setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)-30s - %(levelname)-8s - %(message)s')

fh = logging.FileHandler('test.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(logging.ERROR)

logging.getLogger().addHandler(fh)
logging.getLogger().addHandler(ch)

文件test.py

import logging
import logger

logger = logging.getLogger(__name__)
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')

控制台输出:

2014-07-15 09:47:58,171 - __main__                       - ERROR    - Error message
2014-07-15 09:47:58,171 - __main__                       - CRITICAL - Critical message

Test.log内容:

2014-07-15 09:47:58,171 - __main__                       - DEBUG    - Debug message
2014-07-15 09:47:58,171 - __main__                       - INFO     - Info message
2014-07-15 09:47:58,171 - __main__                       - WARNING  - Warning message
2014-07-15 09:47:58,171 - __main__                       - ERROR    - Error message
2014-07-15 09:47:58,171 - __main__                       - CRITICAL - Critical message

推荐答案

看到双重输出的原因是因为您在第一个文件中设置了两个 StreamHandlers. logger.py

The reason you are seeing the double output is because you have set up two StreamHandlers in your first file; logger.py

在此行中明确显示一个:

One explicitly in this line:

ch = logging.StreamHandler()

此行中的另一个:

logging.basicConfig(level=logging.DEBUG,

根据 logging.basicConfig 的文档:

因此,您应该删除basicConfig行.

So you should remove the basicConfig line.

但是,在删除它之后,您需要将根记录程序的级别设置为DEBUG,因为您是在basicConfig行中这样做的:

However after you remove it you will need to set the level for the root logger to DEBUG, since you do this in the basicConfig line:

logging.getLogger().setLevel(logging.DEBUG)

这篇关于禁用根记录器的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:34