blob: e8c916d275b438406356b20f94a8138276a48cf8 [file] [log] [blame]
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03001import logging
koder aka kdanilov73084622016-11-16 21:51:08 +02002from typing import Callable, IO, Optional
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03003
4
koder aka kdanilov22d134e2016-11-08 11:33:19 +02005def color_me(color: int) -> Callable[[str], str]:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03006 RESET_SEQ = "\033[0m"
7 COLOR_SEQ = "\033[1;%dm"
8
9 color_seq = COLOR_SEQ % (30 + color)
10
11 def closure(msg):
12 return color_seq + msg + RESET_SEQ
13 return closure
14
15
16class ColoredFormatter(logging.Formatter):
17 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
18
19 colors = {
20 'WARNING': color_me(YELLOW),
21 'DEBUG': color_me(BLUE),
22 'CRITICAL': color_me(YELLOW),
23 'ERROR': color_me(RED)
24 }
25
koder aka kdanilov22d134e2016-11-08 11:33:19 +020026 def __init__(self, msg: str, use_color: bool=True, datefmt: str=None) -> None:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030027 logging.Formatter.__init__(self, msg, datefmt=datefmt)
28 self.use_color = use_color
29
koder aka kdanilov22d134e2016-11-08 11:33:19 +020030 def format(self, record: logging.LogRecord) -> str:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030031 orig = record.__dict__
32 record.__dict__ = record.__dict__.copy()
33 levelname = record.levelname
34
35 prn_name = levelname + ' ' * (8 - len(levelname))
36 if levelname in self.colors:
37 record.levelname = self.colors[levelname](prn_name)
38 else:
39 record.levelname = prn_name
40
41 # super doesn't work here in 2.6 O_o
42 res = logging.Formatter.format(self, record)
43
44 # res = super(ColoredFormatter, self).format(record)
45
46 # restore record, as it will be used by other formatters
47 record.__dict__ = orig
48 return res
49
50
koder aka kdanilov22d134e2016-11-08 11:33:19 +020051def setup_loggers(def_level: int = logging.DEBUG, log_fname: str = None, log_fd: IO = None) -> None:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030052
53 log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s'
54 colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S")
55
koder aka kdanilov73084622016-11-16 21:51:08 +020056 sh = logging.StreamHandler()
57 sh.setLevel(def_level)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030058 sh.setFormatter(colored_formatter)
koder aka kdanilov73084622016-11-16 21:51:08 +020059
60 logger = logging.getLogger('wally')
61 logger.setLevel(logging.DEBUG)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030062 logger.addHandler(sh)
63
64 logger_api = logging.getLogger("wally.fuel_api")
koder aka kdanilov73084622016-11-16 21:51:08 +020065 logger_api.setLevel(logging.WARNING)
66 logger_api.addHandler(sh)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030067
koder aka kdanilov22d134e2016-11-08 11:33:19 +020068 if log_fname or log_fd:
69 if log_fname:
koder aka kdanilov73084622016-11-16 21:51:08 +020070 handler = logging.FileHandler(log_fname) # type: Optional[logging.Handler]
koder aka kdanilov22d134e2016-11-08 11:33:19 +020071 else:
72 handler = logging.StreamHandler(log_fd)
73
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030074 log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s'
75 formatter = logging.Formatter(log_format, datefmt="%H:%M:%S")
koder aka kdanilov22d134e2016-11-08 11:33:19 +020076 handler.setFormatter(formatter)
77 handler.setLevel(logging.DEBUG)
koder aka kdanilov73084622016-11-16 21:51:08 +020078
koder aka kdanilov22d134e2016-11-08 11:33:19 +020079 logger.addHandler(handler)
80 logger_api.addHandler(handler)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030081
koder aka kdanilov73084622016-11-16 21:51:08 +020082 logging.getLogger('paramiko').setLevel(logging.WARNING)