简体   繁体   English

在类方法中定义时,不会在 DEBUG 模式下打印 INFO 级别的日志消息

[英]INFO level logging message not printed on DEBUG mode when defined inside class method

I just started experimenting with the logging module in python and I find it really nice.我刚开始在 python 中试验日志模块,我发现它非常好。 I run into a weird problem though.不过我遇到了一个奇怪的问题。

I know that when on DEBUG mode logs from all levels should be outputted to console.我知道在调试模式下,所有级别的日志都应该输出到控制台。 But this doesn't seem to be the case.但情况似乎并非如此。

Do you see anything wrong with the code bellow?你看到下面的代码有什么问题吗?

import logging
import os

class MyClass():
    def __init__(self):
        self.logger = logging.getLogger(
                self.__class__.__name__ + '_{0}'.format(os.getpid()))
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        # create formatter
        formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        # add formatter to ch
        ch.setFormatter(formatter)
        # add ch to logger
        self.logger.addHandler(ch)
        self.logger.info('server is up and running, congrats!')

In my understanding when creating an instance of MyClass an info message should be printed to my console.根据我的理解,在创建MyClass实例时,应将信息消息打印到我的控制台。 But that doesn't happen for some reason.但由于某种原因,这不会发生。 If I change the level of the message to error then I get the expected output.如果我将消息级别更改为error那么我会得到预期的输出。

If I follow the same procedure for creating the logger but not inside a class member method, then it works.如果我遵循相同的过程来创建记录器但不在类成员方法中,那么它就可以工作。

You have to setLevel to your logger self.logger as well not only your handler ch您必须将self.logger为您的记录器self.logger不仅是您的处理程序ch

import logging
import os

class MyClass():
    def __init__(self):
        self.logger = logging.getLogger(
                self.__class__.__name__ + '_{0}'.format(os.getpid()))
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        self.logger.setLevel(logging.DEBUG)
        # create formatter
        formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        # add formatter to ch
        ch.setFormatter(formatter)
        # add ch to logger
        self.logger.addHandler(ch)
        self.logger.debug('server is up and running, congrats!')


myclass = MyClass()

The output is:输出是:

2016-03-24 10:11:44,319 - MyClass_73042 - DEBUG - server is up and running, congrats!

My own Logger(use StreamHandler and FileHandler, the log lines show on console and write to file as well):我自己的记录器(使用 StreamHandler 和 FileHandler,日志行显示在控制台上并写入文件):

import logging
import os


class Logger:
    DEFAULT_LOG_OUTPUT = "/home/haifzhan/"

    def __init__(self, logger_name, log_file_name, log_dir=DEFAULT_LOG_OUTPUT, log_level=logging.DEBUG):
        self.logger = logging.getLogger(logger_name,)
        self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

        self.file_handler = logging.FileHandler(os.path.join(log_dir, log_file_name))
        self.file_handler.setFormatter(self.formatter)
        self.logger.setLevel(log_level)
        self.logger.addHandler(self.file_handler)

        self.console_handler = logging.StreamHandler()
        self.console_handler.setFormatter(self.formatter)
        self.console_handler.setLevel(logging.DEBUG)
        self.logger.addHandler(self.console_handler)

    def get_logger(self):
        return self.logger

To use it, put below at the beginning of your script:要使用它,请将其放在脚本的开头:

logger = Logger("my logger", "logfile.log", log_dir="/path/to/", log_level=logging.INFO).get_logger()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Python记录器无法在logging.INFO级别显示信息或调试消息? - Why doesn't Python logger display info or debug message at level logging.INFO? 如何将 INFO 和 DEBUG 日志消息发送到 stdout 并将更高级别的消息发送到 stderr - How can INFO and DEBUG logging message be sent to stdout and higher level message to stderr 日志记录 - 将级别从 INFO 更改为 DEBUG 会禁用所有日志记录消息 - Logging - changing level from INFO to DEBUG disables all logging messages 如何将调试级别记录到文件,但仅在python屏幕上记录信息级别 - how to logging debug level to file but only info level on screen in python 当 dictConfig 设置级别时,日志记录不显示调试级别 - Logging not displaying debug level when level is set by dictConfig 将日志级别设置为logging.DEBUG或logging.INFO无效 - Setting log level to logging.DEBUG or logging.INFO has no effect 从线程登录:登录到INFO或更高版本时退出线程,登录到DEBUG时正常运行 - Logging from thread: Thread exits when logging to INFO and higher, runs normally when logging to DEBUG python将信息级别记录到文件 - python logging info level to a file Python 日志记录:即使处理程序级别为 INFO,调试消息也会记录到 stderr - Python logging: debug messages logged to stderr even though handler level is INFO python 日志记录设置调试级别 - python logging setting debug level
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM