0

I am trying to learn logger and I have written a way to print logs on the console and file. I set the log level to DEBUG for both the handlers. DEBUG logs are not getting printed in console and in file. ERROR/WARNING levels are getting printed as expected.

Below is the code

import logging
logger = logging.getLogger()
fileHandler = logging.FileHandler("log1")
fileHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(name)-36s %(asctime)s %(levelname)-8s: %(message)s")
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)


fileHandler1 = logging.StreamHandler()
fileHandler1.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(name)-36s %(asctime)s %(levelname)-8s: %(message)s")
fileHandler1.setFormatter(formatter)
logger.addHandler(fileHandler1)

logger.debug("My name is nitesh")
1
  • 2
    You have set the logging level for the handlers, but not for the logger itself. Use e.g. logger.setLevel(logging.DEBUG). Commented Mar 2, 2020 at 13:33

1 Answer 1

3

You need to also set the level on the logger itself.

logger.setLevel(logging.DEBUG)

The documentation has a nice flowchart about this.

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

2 Comments

Thanks for the information. It worked. This information is missing in many logging documentation websites like RealPython where they are only showing Warning and error because default level is warning
The python.org logging documentation is pretty good and I can only recommend to use it.

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.