0

Here is a reproducible code snippet:

import logging

logger=logging.getLogger('test')
#create a stream handler for logging out to console.
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.ERROR)

#create a file handler for logging to file
file_handler = logging.FileHandler('logs',mode='a')
file_handler.setLevel(logging.DEBUG)

logger.addHandler(file_handler)
logger.addHandler(stream_handler)

logger.info("this is info")

I am expecting the log to appear in the file named logs however it never writes the log to that file. What am I missing here?

1 Answer 1

2

When you create a logger, its logging level is NOTSET, thus it inherits its level from root logger (i.e. its parent logger), which has the logging level WARNING by default and info has a lower level so its not passed to the handlers. Try setting the logger level as well with

logger.setLevel(logging.DEBUG)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.