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

5 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/MezzoScettico 2h ago

My desire is that INFO subdivides into INFO1, INFO2, INFO3, etc and that INFO, DEBUG, WARN was not enough granularity.

logging.warn(“This message shows up for logging levels DEBUG, INFO, and WARN, but not for ERROR or CRITICAL.”)

OK, but what if I don't want all the INFO messages? Sometimes I want only the higher level, sometimes I want really verbose detail. I typically have at least 0-2, and often more.

I know, I know. RTFM. I'm reading, still trying to absorb the model. Another answer says that there are actually level numbers and they are a lot finer granularity. Maybe that's the answer.

It’s also handy that you can setup multiple loggers at different levels, so, eg, you might set screen output level to WARN and save a log file with the level set to INFO.

The multiple-logger thing seems like a powerful mechanism and I think that's what I really want to understand. It's overkill for what I'm trying to do today but I'm trying to practice with some baby code to figure it out.

3

u/cointoss3 2h ago

I feel like this is an XY problem. Why do you feel like you need 3 different info log levels?

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.

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.

•

u/Outside_Complaint755 17m ago

Instead of sending all messages to the terminal, send the logging output to a file, and then you can use Ctrl+F to search the file for whatever specific message you are looking for.  You can then include keys to search for (I1, I2, etc) in the log messages instead of trying to insert a dozen custom logging levels.