13 - Fixing logging and processing data
Today I waded through the quagmire of figuring out how to log for a package in Python. I created a function to format a logger and called the function each time I needed a logger, which was in each new module. However, only the first logger would log, and I would miss the rest of the output.
Here’s what it looked like:
|
|
The output would be:
|
|
This method creates a new root logger each time, so the app loses subsequent logger calls.
# Solution
Here’s a very simple solution I found:
|
|
Output:
|
|
The logging.basicConfig
line creates the root logger and should go as far toward the beginning of your application as possible. Then each module - or subsequent .py
file imported within your application - should have the logger = logging.getLogger(__name__)
line. This line will create a new child logger with the module’s name, making it easy to debug.
After looking through many resources to solve this problem, this was the most straightforward explanation I could find sans techie details: loggly.com.
I'm a freelance software developer located in Denver, Colorado. If you're
interested in working together or would just like to say hi you can reach me
at me@
this domain.