使用Python日志记录管理记录器 [英] Managing loggers with Python logging

查看:110
本文介绍了使用Python日志记录管理记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个服务器应用程序,该应用程序应该能够在控制台和日志文件上以不同级别登录.

I'm writing a server app which should be able to log at different levels both on the console and a log file.

问题是,如果设置了logging.basicConfig(),它将登录到控制台,但是必须在主线程中进行设置.

The problem is, if logging.basicConfig() is set it will log to the console but it must be set in the main thread.

还可以使用logging.basicConfig(filename ='logger.log')进行设置,以写入文件.

It can also be set with logging.basicConfig(filename='logger.log') to write to a file.

设置用于控制台日志记录(logging.StreamHandler())或文件日志记录(logging.FileHandler())的句柄是对logging.baseconfig()选项集的补充.

Setting a handle either for console logging (logging.StreamHandler()) or file logging (logging.FileHandler()) complements the logging.baseconfig() option set.

问题是设置不是独立的. 我的意思是,logging.baseConfig()的日志级别必须包含处理程序级别,否则它将不会被记录.

The problem is, that the settings are not independent. What I mean is, the loglevel of logging.baseConfig() must include the Handler level, or it wont be logged.

因此,如果我将baseConfig设置为登录到文件,将StreamHandler设置为登录到控制台,则文件日志级别必须低于控制台级别. (此外,basicConfig选项还记录所有其他日志.)

So if I set the baseConfig to log to file, and a StreamHandler to log to console, the file loglevel must be lower than the console level. (Also, the basicConfig option logs all other logs.)

我试图创建两个句柄,一个用于控制台,一个用于日志文件,它们可以工作,但是basicConfig()指定的任何日志类型仍将显示重复消息.

I tried to create two Handles, one for the console and one for the log file, they work, but whatever log type is specified by basicConfig() will still be displayed duplicating messages.

有没有办法禁用basicConfig()的输出? 还是采用其他方式来实现这些选项?

Is there a way to disable the output of basicConfig() ? Or any other way to implement these options ?

谢谢.

推荐答案

您没有在问题中说出您想要在控制台和文件记录上使用什么级别.但是,您不必调用basicConfig(),因为它只是一个便捷功能.您可以例如(刚刚输入的代码,未经测试):

You don't say in your question exactly what levels you want on your console and file logging. However, you don't need to call basicConfig(), as it's only a convenience function. You can do e.g. (code just typed in, not tested):

import logging

logger = logging.getLogger(__name__)
configured = False

def configure_logging():
    global configured
    if not configured:
        logger.setLevel(logging.DEBUG) # or whatever
        console = logging.StreamHandler()
        file = logging.FileHandler('/path/to/file')
        #set a level on the handlers if you want;
        #if you do, they will only output events that are >= that level
        logger.addHandler(console)
        logger.addHandler(file)
        configured = True

首先将事件传递给记录器,如果要处理事件(由于比较记录器和事件的级别),则将该事件作为记录传递给记录器的每个处理程序及其所有祖先的处理程序出色地.如果在处理程序上设置了级别,则该处理程序可能会丢弃该事件,否则它将输出该事件.

Events are passed to the logger first, and if an event is to be processed (due to comparing the level of the logger and the event) then the event is passed to each handler of the logger and all its ancestors' handlers as well. If a level is set on a handler the event may be dropped by that handler, otherwise it will output the event.

这篇关于使用Python日志记录管理记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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