r/learnpython • u/MezzoScettico • 3h 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
1
u/Explorer651 1h ago
Custom levels are possible. The logging HOWTO says Logger.log takes the level as an explicit argument, which is how you log at custom levels, and it gives the numeric values so you can pick your own relative to the built-in ones. It also says the existing levels should usually be enough, so keep custom ones to a minimum.