Created
August 15, 2012 23:17
-
-
Save scardine/3364608 to your computer and use it in GitHub Desktop.
python script bootstrap
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 logging | |
import argparse | |
import datetime | |
import sys | |
FORMAT = '%(asctime)-15s [%(levelname)s] %(message)s' | |
LOGLEVEL = logging.INFO | |
parser = argparse.ArgumentParser(description='Program descriptiom.') | |
parser.add_argument('-sf', '--someflag', const=True, action='store_const', help='Option description') | |
parser.add_argument('-ao', '--another-option', type=int, help='Another option') | |
parser.add_argument('-f', '--logfile', type=str, help='Log file name (default: stderr)') | |
parser.add_argument('-l', '--loglevel', type=str, help='Log level, CRITICAL, ERROR, WARNING, INFO or DEBUG (default: INFO)') | |
args = parser.parse_args() | |
# LOGGING | |
logfile = sys.stderr | |
if args.logfile: | |
print "Logging to", args.logfile | |
logfile = open(args.logfile, 'a') | |
if args.loglevel: | |
level = getattr(logging, args.loglevel.upper(), None) | |
if level not in (logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG): | |
raise ValueError('Invalid log level (%s=%s). Should be CRITICAL, ERROR, WARNING, INFO or DEBUG.' % (args.loglevel, level)) | |
else: | |
level = LOGLEVEL | |
logging.basicConfig(file=logfile, level=level, format=FORMAT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment