Python日志记录:INFO,DEBUG日志未显示 [英] Python Logging: INFO,DEBUG Logs not displayed

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

问题描述

Python版本:2.7

Python Version : 2.7

我正在使用以下代码在控制台上显示日志.但是,不会显示INFO和DEBUG日志.

I am using the below code to display the logs on console. However, INFO and DEBUG logs are not displayed.

代码

import logging
class LogTest():
    def __init__(self):
        logger_obj = logging.getLogger('Sample Logger')
        console_logger = logging.StreamHandler()
        console_logger.setLevel(logging.INFO)

        logger_obj.addHandler(console_logger)
        logger_obj.info('INFO LOG')
        logger_obj.debug('DEBUG LOG')
        logger_obj.error('ERROR LOG')
        logger_obj.warning('WARNING LOG')
        logger_obj.critical('CRITICAL LOG')

if __name__ == '__main__':
    log_instance = LogTest()

输出

ERROR LOG
WARNING LOG
CRITICAL LOG

根据 python文档,在设置的日志上方记录级别应显示.谁能解释为什么会这样?

According to python documentation, logs above the set logging level should be displayed. Can anyone explain why this is happening?

此外,我应该如何启用DEBUG和INFO日志?

Also, How should I enable DEBUG and INFO logs?

推荐答案

  1. 您要为流处理程序设置日志级别,而必须为记录器本身设置日志级别(在本例中为 logger_obj ).Handler用于将其他过滤器应用于日志,但首先由记录器本身对其进行过滤.

  1. You're setting log level for stream handler while you have to do it for logger itself (logger_obj in your case). Handler is used to apply additional filters to logs, but they are first filtered by logger itself.

调试>INFO,因此必须将级别设置为DEBUG,而不是INFO(如果要查看所有日志).

DEBUG > INFO, so you have to set level to DEBUG, not INFO (If you want to see all logs).

简而言之,使用:

logger_obj.setLevel(logging.DEBUG)

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

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