blob: 4c1c02c38aa631838ec58edfa60c7a561c2c5e22 [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import logging
Alex3ebc5632019-04-18 16:47:18 -05002import os
savex4448e132018-04-25 15:51:14 +02003
Alex Savatieiev5118de02019-02-20 15:50:42 -06004pkg_dir = os.path.dirname(__file__)
5pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir)
6pkg_dir = os.path.normpath(pkg_dir)
7pkg_dir = os.path.abspath(pkg_dir)
8
savex4448e132018-04-25 15:51:14 +02009
10def color_me(color):
11 RESET_SEQ = "\033[0m"
12 COLOR_SEQ = "\033[1;%dm"
13
14 color_seq = COLOR_SEQ % (30 + color)
15
16 def closure(msg):
17 return color_seq + msg + RESET_SEQ
18 return closure
19
20
21class ColoredFormatter(logging.Formatter):
22 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
23
24 colors = {
25 'INFO': color_me(WHITE),
26 'WARNING': color_me(YELLOW),
27 'DEBUG': color_me(BLUE),
28 'CRITICAL': color_me(YELLOW),
29 'ERROR': color_me(RED)
30 }
31
32 def __init__(self, msg, use_color=True, datefmt=None):
33 logging.Formatter.__init__(self, msg, datefmt=datefmt)
34 self.use_color = use_color
35
36 def format(self, record):
37 orig = record.__dict__
38 record.__dict__ = record.__dict__.copy()
39 levelname = record.levelname
40
41 prn_name = levelname + ' ' * (8 - len(levelname))
42 if levelname in self.colors:
43 record.levelname = self.colors[levelname](prn_name)
44 else:
45 record.levelname = prn_name
46
47 # super doesn't work here in 2.6 O_o
48 res = logging.Formatter.format(self, record)
49
50 # res = super(ColoredFormatter, self).format(record)
51
52 # restore record, as it will be used by other formatters
53 record.__dict__ = orig
54 return res
55
56
57def setup_loggers(name, def_level=logging.DEBUG, log_fname=None):
58
59 # Stream Handler
60 sh = logging.StreamHandler()
61 sh.setLevel(def_level)
62 log_format = '%(message)s'
63 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
64 sh.setFormatter(colored_formatter)
65
66 # File handler
67 if log_fname is not None:
68 fh = logging.FileHandler(log_fname)
69 log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s'
70 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
71 fh.setFormatter(formatter)
72 fh.setLevel(logging.DEBUG)
73 else:
74 fh = None
75
76 logger = logging.getLogger(name)
77 logger.setLevel(logging.DEBUG)
78 if len(logger.handlers) == 0:
79 logger.addHandler(fh)
80
81 logger_cli = logging.getLogger(name + ".cli")
Alex Savatieiev799bee32019-02-20 17:19:26 -060082 logger_cli.setLevel(logging.INFO)
savex4448e132018-04-25 15:51:14 +020083 if len(logger_cli.handlers) == 0:
84 logger_cli.addHandler(sh)
85
86 return logger, logger_cli
Alex Savatieiev5118de02019-02-20 15:50:42 -060087
Alex3ebc5632019-04-18 16:47:18 -050088
Alex Savatieiev5118de02019-02-20 15:50:42 -060089# init instances of logger to be used by all other modules
90logger, logger_cli = setup_loggers(
91 'cfg_checker',
92 log_fname=os.path.join(
93 pkg_dir,
Alex9a4ad212020-10-01 18:04:25 -050094 os.getenv('MCP_LOGFILE', 'cfg_checker.log')
Alex Savatieiev5118de02019-02-20 15:50:42 -060095 )
96)