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 
6
  • I think this covers the topic pretty comprehensively: stackoverflow.com/questions/17668633/… Commented Dec 7, 2021 at 18:55
  • Does this answer your question? What is the point of setLevel in a python logging handler? Commented Dec 7, 2021 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? Commented Dec 7, 2021 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. Commented Dec 7, 2021 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... Commented Dec 7, 2021 at 19:30

1 Answer 1

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') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.