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
1
u/Lumethys 1h 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