0

I copied example from real python, and added two more rows to log also info & debug level messages. However, they never get logged. Do you know what is wrong?

import logging

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

# Create handlers
c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file.log')
c_handler.setLevel(logging.INFO)
f_handler.setLevel(logging.DEBUG)

# Create formatters and add it to handlers
c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)

# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)

logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is info')
logger.debug('Just debugging')

This results to a log file with following content, i.e. info and debug messages are missing.

2021-12-07 20:40:16,301 - __main__ - WARNING - This is a warning
2021-12-07 20:40:16,301 - __main__ - ERROR - This is an error
MattiH
  • 554
  • 5
  • 9
  • I think this covers the topic pretty comprehensively: https://stackoverflow.com/questions/17668633/what-is-the-point-of-setlevel-in-a-python-logging-handler – Weeble Dec 07 '21 at 18:55
  • Does this answer your question? [What is the point of setLevel in a python logging handler?](https://stackoverflow.com/questions/17668633/what-is-the-point-of-setlevel-in-a-python-logging-handler) – Weeble Dec 07 '21 at 18:57
  • Thanks @Weeble but actually I don't get the point from it. I do exactlyy as proposed in the first answer there, in my opinion, but got a different answer. Could you point out more clearly what is wrong with my code above? – MattiH Dec 07 '21 at 19:06
  • Your example never sets the root logger's level, only the handlers' levels. The answers in the question I linked explain that it is the root logger's level that needs to be set. – Weeble Dec 07 '21 at 19:21
  • Actually, even if I added the root level with one line of code, the problem persisted. However, it looks like I have now found something else that helps... – MattiH Dec 07 '21 at 19:30
  • This one helped me to the right path - longer explanation below https://stackoverflow.com/questions/6652727/different-logging-levels-for-filehandler-and-display-in-python Thanks for your help! – MattiH Dec 07 '21 at 19:36

1 Answers1

0

Now I finally got answer, by applying guidance giving here. I don't know exactly why my approach above doesn't work. However, I know how to make it work. I removed the FileHandler altogether, and added the logging to the file in the basic config like below. Now I get debug level logging into file, and info level logging into the console

import logging

f_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'

logging.basicConfig(level=logging.DEBUG,
                    format=f_format,
                    filename='file.log',
                    filemode='w')

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

c_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.INFO)
c_handler.setFormatter(c_format)
logger.addHandler(c_handler)

logger.warning('This is a warning')
logger.error('This is an error')
logger.info('This is info')
logger.debug('Just debugging')
MattiH
  • 554
  • 5
  • 9