r/learnpython • • 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.

4 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/Boothiepro 2h ago

Indeed you can. You can add more levels between 10-debug and 20-info, or you can assign them to different handlers. handlers are where the logs go, e.g. filehandler, just the default stdout console handler etc. You can assign multiple console handlers, pipe the logs into each of them based on your criteria, and turn them on/off based on your needs. handlers are fun, i wrote one to flash my rgb gaming mouse when an error occurs or send me a Discord message. You can format these messages to show the offending error line, function called, file ran, color these fields accordingly etc. With this info open a link to the github repo's specific file and code line to check what errored when i'm not on my computer.