Skip to content

Instantly share code, notes, and snippets.

@slmingol
Forked from ms5/verbos-argpary-example.py
Created December 20, 2019 18:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmingol/6d018551c78d01b79931ca5973251aa7 to your computer and use it in GitHub Desktop.
Save slmingol/6d018551c78d01b79931ca5973251aa7 to your computer and use it in GitHub Desktop.
manipulating log level with python argparse
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')
@slmingol
Copy link
Author

Thanks for posting this!! I used a level of 40 instead, so that logging.info() messages trigger from the first -v args.verbose = 40 - (10*args.verbose) if args.verbose > 0 else 0

Best :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment