Logging everything to file and only DEBUG messages to console in python

Question:

I want logger to log everything to file but only debug messages to console. This answer suggests creating custom handler. I tried following:

import logging 
import sys

class DebugConsoleHandler(logging.StreamHandler):
    def __init__(self):
        super().__init__(stream = sys.stdout)
    
    def emit(self, record):
        if not record.levelno == logging.DEBUG:
            return
        super().emit(record)

class Module1():
    def __init__(self) -> None:
        self.logFilePath = 'logging_exp.txt'
        self.configureLogger()        

    def configureLogger(self):
        self.logger = logging.getLogger(__name__)

        # log time and message to file
        formatter1 = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
        file_hander = logging.FileHandler(self.logFilePath)
        file_hander.setLevel(logging.INFO)
        file_hander.setFormatter(formatter1)        

        # log only message to console
        formatter2 = logging.Formatter("%(levelname)s %(message)s")
        console_handler = DebugConsoleHandler()
        console_handler.setFormatter(formatter2)

        self.logger.addHandler(file_hander)
        self.logger.addHandler(console_handler)

    def testLogging(self):
        a = 5
        self.logger.info('Starting up ...')
        self.logger.debug('a:%s', a)
  
m = Module1()
m.testLogging()

This only created file logging_expt.txt, but did not log anything to anywhere, neither to file nor to console. What I am missing?

Asked By: MsA

||

Answers:

You need to set the logger’s level to debug as well:

self.logger.setLevel(logging.DEBUG)

at the end of configureLogger().

That will also be the reason that without that, logging_exp.txt will be empty: the default logging level is WARNING, so the info messages won’t be logged to file either.

(Tip: for debugging purposes, it can be good to also add a warning level log statement somewhere. These should be emitted for the default logging level, which helps narrowing down problems.)

Answered By: 9769953
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.