r/learnpython • u/MezzoScettico • 2h ago
Can logging replace my verbosity controls?
An old habit from C days is to add print statements for debug and assign them different verbosity levels. I've been doing that in Python as well.
if verbosity > 2:
print("This level generates a lot of output, so use it sparingly")I've been told that the logging facility is the Python way to do this, so I'm finally investigating it. What I'm seeing is that the levels are DEBUG, INFO, WARNING, ERROR which doesn't really fit what I want to happen. Everything I want to put under verbosity control is technically DEBUG or INFO.
So my question is: are there sublevels on the DEBUG or INFO logging? Or is there another approach for this sort of thing?
EDIT: Still reading. I think the answer might be a hierarchy of loggers and filters? Trying to figure out how that works now.
View all comments
Show parent comment
3
u/cointoss3 2h ago
Each level is a value and when you turn on that level, you’ll get everything above it. DEBUG gets you debug, info, warn, error, and critical. ERROR gets you error and critical. You can also set flags that can filter records. You can also have multiple loggers and turn them on and off. Each file (module) can have its own scoped logger that you turn on or off. The logging module has a lot it can do and lots of flexibility but it’s a little confusing at first.
This is a video I felt like I learned from, even though I thought I knew how to use the logging package: https://www.youtube.com/watch?v=9L77QExPmI0