Quickly enable robust Python logging throughout an application

Sometimes it’s helpful to quickly enable logging in a Python application so you can peer into what the libraries you depend on are doing under the hood. Many libraries include logging, but in order to see the messages they rely on you configuring a root level logger in your application’s “main”.

It took me longer than I’d hoped to get this set up for a script I was working on to test out some ideas, so hopefully this post will save someone else some time.

In your application or script’s main file or thread:

import logging

logging.basicConfig(**{
    'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    'level': logging.DEBUG
})
logger = logging.getLogger()

Leave a Reply

Your email address will not be published. Required fields are marked *