The Pythonic Guide To Logging

Sid Panjwani on June 25, 2018

When done properly, logs are a valuable component of your observability suite. Logs tell the story of how data has changed in your application. T... [Read Full]
markdown guide
 

First of all, wonderful post.
I'm working on a Flask application right now: first time with Python and I was looking for useful information about how to write logs the right way when I saw this post — awesome!

Anyway, I tried to copy & paste the code but it looks like the logger object doesn't care about the format you passed to logging.basicConfig if you are using the FileHandler and you've to explicitly call .setFormatter to make it know.

log_formatter = logging.Formatter('%(asctime)s - %(levelname)s --: %(message)s')

file_handler = logging.FileHandler('logfile')
file_handler.setFormatter(log_formatter)
 

Might wanna look at the docs at the app specific logger flask.pocoo.org/docs/1.0/logging/ it also shows how to do some useful stuff like access library loggers and such

 

Thank you for the useful post. Just a quick correction.

Running the sample code under Logging to a File, the logger is created with a default level of WARNING, so the INFO log record is actually not written to the log file. To include it in the log file, set the logger level to INFO as well:

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
 

Thanks for catching that, I'll leave a note in the article!

 

really a nice write :D would have helped me a lot if I found this a few weeks back :p

code of conduct - report abuse