logging.py (871B)
1 import os 2 import sys 3 import logging.config 4 5 6 def add_arguments(parser): 7 """Add arguments to the given argument argparse parser.""" 8 9 parser.add_argument( 10 "--logging-config", 11 "-c", 12 dest="logging_ini_fname", 13 default=False, 14 help="Location of logging configuration file", 15 ) 16 17 18 def handle_arguments(args): 19 """Take arguments from argparse arguments and configure logging.""" 20 21 if args.logging_ini_fname: 22 if os.path.isfile(args.logging_ini_fname): 23 logging.config.fileConfig(args.logging_ini_fname) 24 else: 25 sys.exit("Can not find logging ini file: %s" % args.logging_ini_fname) 26 27 elif os.path.isfile("logging.ini"): 28 logging.config.fileConfig("logging.ini") 29 30 31 def strip_control_chars(s): 32 if not s: 33 return "" 34 return "".join(i for i in s if 31 < ord(i) < 127)