-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
76 lines (61 loc) · 1.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import logging
logger = logging.getLogger(__name__)
def make_arg_parser():
argument_parser = argparse.ArgumentParser()
# Streams
argument_parser.add_argument(
'-s', '--stream',
choices=['sys.stderr', 'sys.stdout'],
default='sys.stderr'
)
# Levels
level_names = [
level
for level in logging._levelNames.keys()
if isinstance(level, str)
]
argument_parser.add_argument(
'-l', '--level',
choices=sorted(level_names),
default='INFO'
)
# Files
argument_parser.add_argument('-f', '--filename')
argument_parser.add_argument('-m', '--filemode')
# Formats
argument_parser.add_argument('-dfmt', '--datefmt')
argument_parser.add_argument('-fmt', '--msgfmt')
return argument_parser
def extract_config(parsed_args):
config_options = {
'level': logging.INFO
}
if parsed_args.level is not None:
config_options['level'] = logging.getLevelName(parsed_args.level)
if parsed_args.stream is not None:
config_options['stream'] = eval(parsed_args.stream)
if parsed_args.filename is not None:
config_options['filename'] = parsed_args.filename
if parsed_args.filemode is not None:
config_options['filemode'] = parsed_args.filemode
if parsed_args.msgfmt is not None:
config_options['format'] = parsed_args.msgfmt
if parsed_args.datefmt is not None:
config_options['datefmt'] = parsed_args.datefmt
return config_options
def main():
logger.debug('I used .debug!')
logger.info('I used .info!')
logger.warn('I used .warn!')
logger.error('I used .error!')
try:
raise Exception('Some crazy exception!')
except Exception:
logger.exception('I used .execption!')
if __name__ == '__main__':
# For eval parsing.
exec 'import sys'
parser = make_arg_parser()
logging.basicConfig(**extract_config(parser.parse_args()))
main()