Last active
July 19, 2024 12:19
-
-
Save ms5/9f6df9c42a5f5435be0e to your computer and use it in GitHub Desktop.
manipulating log level with python argparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--verbose', '-v', action='count', default=1) | |
args = parser.parse_args() | |
args.verbose = 70 - (10*args.verbose) if args.verbose > 0 else 0 | |
logging.basicConfig(level=args.verbose, format='%(asctime)s %(levelname)s: %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S') | |
logging.debug('im a DEBUG message') | |
logging.info('im a INFO message') | |
logging.warning('im a WARNING message') | |
logging.critical('im a CRITICAL message') | |
logging.error('im a ERROR message') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is so great. Thank you.
I also recommend setting the initial value to 40.