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
1
u/pingveno 1h ago
I've always found that multiple loggers handle most needs. It's better to not make it more complicated before it needs to be. There's a lot of power given to you with loggers, handlers, formatters, and so on.
Once you're comfortable with logging, it's worth looking into structured logging. It uses key-value pairs to structure your logs instead of flat strings. It's much nicer to use a JSON query tool rather than attempt to construct a regex when sifting through a gigabyte of logs. But again, leave that to later.