0
import logging import sys logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) file_handler = logging.FileHandler('test.log') file_handler.setLevel(logging.DEBUG) logger.addHandler(file_handler) console_handler = logging.StreamHandler(sys.stdout) console_handler.setLevel(logging.DEBUG) logger.addHandler(console_handler) logger.info('Info print') logger.debug('Debug print') 

I have simple code above. How come the debug statement doesn't print? It seems like the logLevel is always determined by line 5. The file_handler.setLevel and the console_handler.setLevel seem to do nothing. I want to eventually have debug prints going to the test.log file, and info prints going to the console.

1
  • stdout is sometimes associated with a terminal. Your terminal is almost never a console. Referring to stdout as "console output" is a terrible idea. Commented Mar 29, 2022 at 13:40

1 Answer 1

1

The logger and handler levels are both used, but at different times. The logger's level is inspected first, and if the event severity level is >= the logger's level, then the event is passed to handlers. The event's level is then checked against each handler's level to determine if that handler handles the event.

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.