Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python command line settable logging verbosity example.
#!/usr/bin/env python2.7
import argparse
import logging
def type_loglevel(level):
try:
result = getattr(logging, level.upper())
except AttributeError:
raise argparse.ArgumentTypeError("'%s' is not a valid log level. Please use %s" % \
(level, [x for x in logging._levelNames.keys() if isinstance(x, str)]))
return result
parser = argparse.ArgumentParser(description='My description goes here')
parser.add_argument('-l', '--loglevel', type=type_loglevel, default='INFO',
help='Log level verbosity (default: INFO)')
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
logging.debug('This is a test debug message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment