Created
August 8, 2013 19:05
-
-
Save gene1wood/6187660 to your computer and use it in GitHub Desktop.
Python command line settable logging verbosity example.
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
#!/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