savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import logging |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 2 | import os |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 3 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 4 | pkg_dir = os.path.dirname(__file__) |
| 5 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
| 6 | pkg_dir = os.path.normpath(pkg_dir) |
| 7 | pkg_dir = os.path.abspath(pkg_dir) |
| 8 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 9 | |
| 10 | def 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 | |
| 21 | class 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 | |
| 57 | def 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 Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 82 | logger_cli.setLevel(logging.INFO) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 83 | if len(logger_cli.handlers) == 0: |
| 84 | logger_cli.addHandler(sh) |
| 85 | |
| 86 | return logger, logger_cli |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 87 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 88 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 89 | # init instances of logger to be used by all other modules |
| 90 | logger, logger_cli = setup_loggers( |
| 91 | 'cfg_checker', |
| 92 | log_fname=os.path.join( |
| 93 | pkg_dir, |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 94 | os.getenv('MCP_LOGFILE', 'cfg_checker.log') |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 95 | ) |
| 96 | ) |