Logging levels dont work for debug and info

Question:

I use the following code in my __init__ class:

# Create a custom logger
self.logger = logging.getLogger(__name__)

# Create handlers
self.handler_cmdline = logging.StreamHandler()
self.handler_file = logging.FileHandler(self.logfile)
self.handler_cmdline.setLevel(logging.DEBUG)
self.handler_file.setLevel(logging.INFO)

# Create formatters and add it to handlers
log_format = logging.Formatter(fmt='%(asctime)s - %(name)s - %(<levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
self.handler_cmdline.setFormatter(log_format)
self.handler_file.setFormatter(log_format)

# Add handlers to the logger
self.logger.addHandler(self.handler_cmdline)
self.logger.addHandler(self.handler_file)

self.logger.debug('Initialisation Complete')
self.logger.info('Initialisation Complete')
self.logger.warning('Initialisation Complete')
self.logger.critical('Initialisation Complete')

The debug and the warning don’t work somehow. Everything from warning and above works.

What’s wrong here?

Asked By: smyril

||

Answers:

import logging

class example():
       def __init__(self):
              self.logger = logging.getLogger(__name__)
              self.logfile = 'example.log'

              # Create handlers
              self.handler_cmdline = logging.StreamHandler()
              self.handler_file = logging.FileHandler(self.logfile)
              self.handler_cmdline.setLevel(logging.DEBUG)
              self.handler_file.setLevel(logging.INFO)


              # Create formatters and add it to handlers
              log_format = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
              self.handler_cmdline.setFormatter(log_format)
              self.handler_file.setFormatter(log_format)

              # Add handlers to the logger
              self.logger.addHandler(self.handler_cmdline)
              self.logger.addHandler(self.handler_file)
              self.logger.setLevel(logging.DEBUG)
              # print(self.logger.level)

              self.logger.debug('Initialisation Complete')
              self.logger.info('Initialisation Complete')
              self.logger.warning('Initialisation Complete')
              self.logger.critical('Initialisation Complete')

example()

Small snippet to test the fix with your code, the issue is that your logger does not have a level set. Because of which default level is used which causes the INFO and DEBUG levelled logs to be ignored. You can try printing the self.logger.level to see what level it is currently set to.

In your case, you did not have logging level set for your self.logger.

Just adding the self.logger.setLevel(logging.DEBUG) fixes the issue. And you can see this output in the console –

2022-12-12 17:44:23 - __main__ - DEBUG - Initialisation Complete
2022-12-12 17:44:23 - __main__ - INFO - Initialisation Complete
2022-12-12 17:44:23 - __main__ - WARNING - Initialisation Complete
2022-12-12 17:44:23 - __main__ - CRITICAL - Initialisation Complete

while in the file, the debug log would not be there, because of the logging level of the file handler being INFO.

The level of the logger should be minimum of the level of the log handlers of that logger, else those logs would not be logged.(e.g. in this case, if you set the logger level to INFO, the debug log will not be printed in stdout either, even though the log handler has its level set to DEBUG.

Answered By: Jay
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.