Python日志记录:仅显示调试级别的信息 [英] Python logging: display only information from debug level

查看:228
本文介绍了Python日志记录:仅显示调试级别的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的第一个Python日志记录实验(2.7),我将创建一个基本的日志记录模块,该模块将快速为其创建记录器(因此,我不必按模块/类对其进行配置).

For my first logging experiment in Python (2.7) I'm creating a basic logging module, which will quickly create a logger for (so I won't have to configure it per module/class).

我要做的是创建一个记录器,该记录器在控制台上显示INFO级别及更高级别的所有消息,并将所有DEBUG级别的信息移至文本文件.

What I want to do is to create a logger which displays all messages of level INFO and higher on the console, and moves all DEBUG level information to a text file.

到目前为止,我已经创建了一个记录器,该记录器将打印INFO并显示到控制台,并将每个日志消息都打印到一个文本文件中.但是,我不希望文本文件中包含所有的INFO和更高级别的消息.

Thus far I have create a logger which prints INFO and up to the console, and which prints every log messages to a text file. However, I don't want all the INFO and higher messages in my text file.

我制作了一个 Python小提琴,其中包含我的代码.

I have made a Python Fiddle which holds my code.

我使用了教程中的以下调试消息:

I used the following debug messages from a tutorial:

log.debug('All systems operational')
log.info('Airspeed 300 knots')
log.warn('Low on fuel')
log.error('No fuel. Trying to glide.')
log.critical('Glide attempt failed. About to crash.')

哪个在控制台上产生:

[INFO] root: Airspeed 300 knots
[WARNING] root: Low on fuel
[ERROR] root: No fuel. Trying to glide.
[CRITICAL] root: Glide attempt failed. About to crash.

在我的debug_log.txt文件中:

And in my debug_log.txt file:

2013-06-14 14:51:46,963:DEBUG:root:All systems operational
2013-06-14 14:51:46,964:INFO:root:Airspeed 300 knots
2013-06-14 14:51:46,964:WARNING:root:Low on fuel
2013-06-14 14:51:46,964:ERROR:root:No fuel. Trying to glide.
2013-06-14 14:51:46,964:CRITICAL:root:Glide attempt failed. About to crash.

在上面的块中,后四行不应位于文件中.我正在尝试做的事情有可能吗?如何获得理想的结果?

In the above block, the latter four lines should not be in the file. Is what I'm trying to do possible and how would I get the desired result?

推荐答案

您必须创建自己的处理程序对象.

从文档中:位于核心日志记录软件包中的FileHandler类将日志记录输出发送到磁盘文件.它继承了StreamHandler的输出功能.因此,这里的想法是扩展FileHandler类,并在emit()方法中,过滤掉所有不在logging.DEBUG级别的record对象.

From the documentation: The FileHandler class, located in the core logging package, sends logging output to a disk file. It inherits the output functionality from StreamHandler. So here the idea is to extend the FileHandler class, and in the emit() method, filter out all record objects that are not in the logging.DEBUG level.

我还没有测试过,但是我认为它可以胜任:

I have not tested this, but I think it would do the job:

from logging import FileHandler, DEBUG
log = logging.getLogger('foo')

class DebugFileHandler(FileHandler):
    def __init__(self, filename, mode='a', encoding=None, delay=False)
        FileHandler.__init__(self, filename, mode, encoding, delay)

    def emit(self, record):
        if not record.levelno == DEBUG:
            return
        FileHandler.emit(self, record)

log.addHandler(DebugFileHandler())

当然,您必须使此代码适应您的代码.而且,说实话,record.level是一个疯狂的猜测,但我认为这是有道理的:-)而且,

Of course, you'll have to adapt this code to your code. And, to be honest, record.level is a wild guess, but I think it makes sense :-) And, I got it right!

在此示例中,处理程序将仅应用于foo记录器.您可能希望根据自己的喜好为您的主处理程序或仅对某些特定处理程序激活它.

In this example the handler will only be applied for the foo logger. You may want to activate it for your main handler, or only for some specific handlers, as you may prefer.

这篇关于Python日志记录:仅显示调试级别的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆