-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_logging.py
49 lines (42 loc) · 1.29 KB
/
cli_logging.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
import argparse
import logging
class ViewCLI:
@classmethod
def get_args(cls):
parser = argparse.ArgumentParser(
description="sample description"
)
parser.add_argument(
"-l",
"--log",
default="warning",
help=("Provide logging level. " "Example --log debug', default='warning'"),
)
return parser.parse_args()
def handle_logging(args):
levels = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warn": logging.WARNING,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
}
level = levels.get(args.log.lower())
if level is None:
raise ValueError(
f"log level given: {args.log}"
f" -- must be one of: {' | '.join(levels.keys())}"
)
if level is None:
raise ValueError(
f"log level given: {args.log}"
f" -- must be one of: {' | '.join(levels.keys())}"
)
LOG_FORMAT = "%(asctime)s:%(levelname)s:%(name)s: %(message)s"
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
if __name__ == "__main__":
args = ViewCLI.get_args()
handle_logging(args)
logging.info("starting...")
logging.debug("Args are : %s", args)