koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 1 | import logging |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 2 | from typing import Callable, IO, Optional |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 3 | |
| 4 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 5 | def color_me(color: int) -> Callable[[str], str]: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 6 | 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 | |
| 16 | class 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 kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 26 | def __init__(self, msg: str, use_color: bool=True, datefmt: str=None) -> None: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 27 | logging.Formatter.__init__(self, msg, datefmt=datefmt) |
| 28 | self.use_color = use_color |
| 29 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 30 | def format(self, record: logging.LogRecord) -> str: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 31 | 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 kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 51 | def setup_loggers(def_level: int = logging.DEBUG, log_fname: str = None, log_fd: IO = None) -> None: |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 52 | |
| 53 | log_format = '%(asctime)s - %(levelname)s - %(name)-15s - %(message)s' |
| 54 | colored_formatter = ColoredFormatter(log_format, datefmt="%H:%M:%S") |
| 55 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 56 | sh = logging.StreamHandler() |
| 57 | sh.setLevel(def_level) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 58 | sh.setFormatter(colored_formatter) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 59 | |
| 60 | logger = logging.getLogger('wally') |
| 61 | logger.setLevel(logging.DEBUG) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 62 | logger.addHandler(sh) |
| 63 | |
| 64 | logger_api = logging.getLogger("wally.fuel_api") |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 65 | logger_api.setLevel(logging.WARNING) |
| 66 | logger_api.addHandler(sh) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 67 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 68 | if log_fname or log_fd: |
| 69 | if log_fname: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 70 | handler = logging.FileHandler(log_fname) # type: Optional[logging.Handler] |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 71 | else: |
| 72 | handler = logging.StreamHandler(log_fd) |
| 73 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 74 | log_format = '%(asctime)s - %(levelname)8s - %(name)-15s - %(message)s' |
| 75 | formatter = logging.Formatter(log_format, datefmt="%H:%M:%S") |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 76 | handler.setFormatter(formatter) |
| 77 | handler.setLevel(logging.DEBUG) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 78 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 79 | logger.addHandler(handler) |
| 80 | logger_api.addHandler(handler) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 81 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 82 | logging.getLogger('paramiko').setLevel(logging.WARNING) |