[英]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.