No solution for "--verbose" (on stdout) output in Pythonds standard library?

Richard Damon Richard at damon-family.org
Wed Jan 4 10:04:33 EST 2023


> On Jan 4, 2023, at 8:56 AM, c.buhtz at posteo.jp wrote:
> 
> Hello,
> 
> first I have to say that in my current and fresh humble opinion the
> often seen "--verbose" switch in command line applications should
> affect only the messages given to the users. This means messages on
> "stdout". That is what this question is about.
> 
> The logging module is not an option because it works on stderr and is
> not intended to offer messages for the user but just for the system and
> its admins via logs (log-files, syslog, stderr redirection, ...).
> 
> Using logging handlers redirecting to stdout are considered as
> workarounds by me and also not an option.
> 
> This is not only my opinion but also part of the Python documentation:
> https://docs.python.org/3/howto/logging.html#when-to-use-logging
> 
> I'm so detailed here about that difference between stdout and stderr
> because in the wild (e.g. on StackOverflow) you often find "use logging
> log levels" as a solution for that problem, which IMHO isn't one.
> 
> Now the question:
> From my research on the docs it seems there is no feature in the
> standard library which could help me to implement "--verbose" or
> multiple verbosity levels like "-vvv"?
> I found some workarounds/hacks.
> https://stackoverflow.com/q/5980042/4865723
> But my experience with Python as a Swiss knife is that there is always
> a standard solution for such basic and often reinvented things. I won't
> believe that each Python developer writes its own verbose feature. ;)
> -- 
> https://mail.python.org/mailman/listinfo/python-list

First, I would say you are incorrect that Payton ALWAYS has a standard solution for “basic” problems, as some problems (like this) aren’t actual that easy to just provide a solution.

“Verbosity” levels, need to be defined by an application, so can’t just be provided by Python, but WILL need some effort on by the programmer.

Second, Stack Overflow is NOT a good source of “Truth” about things, it is just a source of what people on Stack Overflow think is truth, which is up to the reader to decide if it is actually usable.

Now, it turns out that you CAN use argparse (or similar libraries) and logging (or similar libraries) to implement a form of verbosity.

Start off by default to NOT have logging enabled (or only enable for “High” level messages).

Different levels of Verbosity can enable lower levels of logging, and perhaps move the logging from the “default” of stderr to stdout, all of these are possible with these libraries.

Whether this is an “abuse” of the logging library or not is up to you are the programmer to decide, no one is making you do it that way, or saying that is the way it should be done, just a way it COULD be done.

Personally, I might consider the logging module for this, or I might just add print statements conditioned on a verbosity level set by the argument parsing module I was using.


More information about the Python-list mailing list