Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easier way to enable verbose logging

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?

like image 939
user1934146 Avatar asked Dec 31 '12 03:12

user1934146


People also ask

Should I enable WIFI verbose logging?

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.

How do I run verbose mode?

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.


1 Answers

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.

like image 66
Matthew Leingang Avatar answered Oct 04 '22 22:10

Matthew Leingang