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.
6
u/danielroseman 2h ago
Levels are just numbers; DEBUG is 10 and CRITICAL is 50. If you need more granularity, then use more numbers.
Although I don't understand your point; if you want to control whether to log more or less verbosely, then logging at DEBUG seems fine.
1
u/auntanniesalligator 2h ago
Not sure I understand what you’re asking. It sounds like your verbosity scale maps to the values associated with INFO, DEBUG, etc. which are compared to the logging commands via inequalities to determine if they apply.
Perhaps you’re unaware that the cumulative effect is handled by same named commands? So instead of querying the logging level directly as the conditional of an if statement, you put in a line like:
logging.warn(“This message shows up for logging levels DEBUG, INFO, and WARN, but not for ERROR or CRITICAL.”)
logging.debug(“This message only shows up if logging level is set to DEBUG.”)
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.
1
u/Atmosck 1h ago
You should definitely use the logging module instead of print statements. The print function has no place in real code.
The log levels are based on the type of information they carry. If you want to have multiple levels of verbosity for your debug output, I would do what you're doing with an explicit verbosity level and checks, but use
logging.debuginstead ofprint. Or do the same thing withlogging.infoif they are normal user-facing rather than debug logs, but you still want them to have multiple verbosity levels.A slightly more sophisticated way to do that would to create your own wrapper function like
Then set the level when you call VerboseInfoLogger based on command line options or whatever you want to determine it, then pass the log function you created as an argument to things that use it. I do something conceptually similar to this for some simulations I work on where there is a command to trace a single run of the simulation, with varying levels of verbosity. That's a situation where you want multiple levels of what are conceptually INFO logs.
That's just one way to approach it. You can go a lot deeper with the logging module if you want, defining custom log levels or categories.
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.
•
u/Lumethys 56m ago
The logging level are defined by RFC 5424. Not only for Python but for all programming language, yes that does include C
Humanity has yet to find a problem that require your level of fine-grained verbosity.
If you want to filter specific log lines from your logs, you use a Observability tool or platform, like VictoriaLogs, Loki, ElasticSearch,... they have dedicated log query language to filter your logs to any fine-grained level
Personally I prefer VictoriaLog, extremely performance, just a few MBs of RAM, just 1 docker compose line. But you can also look at OpenObserve if the want a 3-in-1 tool in Parquet format and SQL query