简体   繁体   中英

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. 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. But that doesn't happen for some reason. If I change the level of the message to error then I get the expected output.

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

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):

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM