1

My code looks like this

import logging formatter = logging.Formatter("{asctime} - {name} - {levelname} - {message}", "%d-%b-%y %H:%M", "{") def _add_handler(handler: logging.StreamHandler) -> logging.StreamHandler: handler.setFormatter(formatter) handler.setLevel(20) return handler logging.basicConfig( handlers={ _add_handler(logging.FileHandler("filename.log")), _add_handler(logging.StreamHandler()) }) logging.info("hello world") 

What this is supposed to do is log "hello world" both to the console and a file named filename.log, with a severity of INFO, which is what the 20 in the setLevel method is for. However, nothing is being logged at all. Where have I gone wrong?

1 Answer 1

1

You need to set the logging level for the logger as well. By default, it's set to logging.WARNING, so neither handler is seeing the message, let alone determining if it should be handled.

logging.basicConfig( level=logging.INFO, handlers={ _add_handler(logging.FileHandler("filename.log")), _add_handler(logging.StreamHandler()) }) 
Sign up to request clarification or add additional context in comments.

4 Comments

Beat me to it. I was about to answer the exact same thing. Please note the use of logging.INFO instead of the literal 20 - this should be changed in the handler as well.
Eh, I would prefer using the constants, but using an int literal isn't wrong.
Thank you so much! I'm new to Stack Overflow and I've heard a lot of bad stuff about it, so I was lowkey scared about posting here
Glad to see you here despite what you heard, hope you had a good first experience! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.