I want to add a debug print statement test, if I enable --verbose
from the command line and if I have the following in the script.
logger.info("test")
I went through the following questions, but couldn't get the answer...
How to implement the --verbose or -v option into a script?
Python logging - Is there something below DEBUG?
Should You Enable Wi-Fi Verbose Logging? Turning this feature on depends on the situation. If you feel like something wrong is going on, such as slow connection or security issues, it is best to turn it on. But if you feel that your connection at home is fine, there is no need to turn it on.
There are several ways to enable Verbose Mode. During startup, the screen may display which key(s) to press on the keyboard to enable Verbose Mode. Usually, users would press the Esc (escape) key for Linux, or the keyboard shortcut Ctrl + V for Microsoft Windows, and Command + V for macOS.
I find both --verbose
(for users) and --debug
(for developers) useful. Here's how I do it with logging
and argparse
:
import argparse import logging parser = argparse.ArgumentParser() parser.add_argument( '-d', '--debug', help="Print lots of debugging statements", action="store_const", dest="loglevel", const=logging.DEBUG, default=logging.WARNING, ) parser.add_argument( '-v', '--verbose', help="Be verbose", action="store_const", dest="loglevel", const=logging.INFO, ) args = parser.parse_args() logging.basicConfig(level=args.loglevel)
So if --debug
is set, the logging level is set to DEBUG
. If --verbose
, logging is set to INFO
. If neither, the lack of --debug
sets the logging level to the default of WARNING
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With