|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf8 - *- |
| 3 | +"""Log utilities for tmuxp. |
| 4 | +
|
| 5 | +tmuxp.log |
| 6 | +~~~~~~~~~ |
| 7 | +:copyright: Copyright 2013 Tony Narlock. |
| 8 | +:license: BSD, see LICENSE for details |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import absolute_import, division, print_function, with_statement |
| 13 | +import logging |
| 14 | +import time |
| 15 | +from ._vendor.colorama import init |
| 16 | +from ._vendor.colorama import Fore, Back, Style |
| 17 | + |
| 18 | +LEVEL_COLORS = { |
| 19 | + 'DEBUG': Fore.BLUE, # Blue |
| 20 | + 'INFO': Fore.GREEN, # Green |
| 21 | + 'WARNING': Fore.YELLOW, |
| 22 | + 'ERROR': Fore.RED, |
| 23 | + 'CRITICAL': Fore.RED |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +def default_log_template(self, record): |
| 28 | + """Return the prefix for the log message. Template for Formatter. |
| 29 | +
|
| 30 | + :param: record: :py:class:`logging.LogRecord` object. this is passed in |
| 31 | + from inside the :py:meth:`logging.Formatter.format` record. |
| 32 | +
|
| 33 | + """ |
| 34 | + |
| 35 | + tpl = '' |
| 36 | + tpl += Style.RESET_ALL |
| 37 | + tpl += LEVEL_COLORS.get(record.levelname) + Style.BRIGHT + '(%(levelname)s)' + Style.RESET_ALL + ' ' |
| 38 | + tpl += '[' + Fore.BLACK + Style.DIM + Style.BRIGHT + '%(asctime)s' + Fore.RESET + Style.RESET_ALL + ']' |
| 39 | + tpl += ' ' + Fore.WHITE + Style.DIM + Style.BRIGHT + '%(name)s' + Fore.RESET + Style.RESET_ALL + ' ' |
| 40 | + tpl += Style.RESET_ALL |
| 41 | + |
| 42 | + return tpl |
| 43 | + |
| 44 | + |
| 45 | +class LogFormatter(logging.Formatter): |
| 46 | + |
| 47 | + template = default_log_template |
| 48 | + |
| 49 | + def __init__(self, color=True, *args, **kwargs): |
| 50 | + logging.Formatter.__init__(self, *args, **kwargs) |
| 51 | + |
| 52 | + def format(self, record): |
| 53 | + try: |
| 54 | + record.message = record.getMessage() |
| 55 | + except Exception as e: |
| 56 | + record.message = "Bad message (%r): %r" % (e, record.__dict__) |
| 57 | + |
| 58 | + date_format = '%H:%m:%S' |
| 59 | + record.asctime = time.strftime(date_format, self.converter(record.created)) |
| 60 | + |
| 61 | + prefix = self.template(record) % record.__dict__ |
| 62 | + |
| 63 | + formatted = prefix + " " + record.message |
| 64 | + return formatted.replace("\n", "\n ") |
| 65 | + |
| 66 | + |
| 67 | +def debug_log_template(self, record): |
| 68 | + """ Return the prefix for the log message. Template for Formatter. |
| 69 | +
|
| 70 | + :param: record: :py:class:`logging.LogRecord` object. this is passed in |
| 71 | + from inside the :py:meth:`logging.Formatter.format` record. |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + tpl = '' |
| 76 | + tpl += Style.RESET_ALL |
| 77 | + tpl += LEVEL_COLORS.get(record.levelname) + Style.BRIGHT + '(%(levelname)1.1s)' + Style.RESET_ALL + ' ' |
| 78 | + tpl += '[' + Fore.BLACK + Style.DIM + Style.BRIGHT + '%(asctime)s' + Fore.RESET + Style.RESET_ALL + ']' |
| 79 | + tpl += ' ' + Fore.WHITE + Style.DIM + Style.BRIGHT + '%(name)s' + Fore.RESET + Style.RESET_ALL + ' ' |
| 80 | + tpl += Fore.GREEN + Style.BRIGHT + '%(module)s.%(funcName)s()' |
| 81 | + tpl += Fore.BLACK + Style.DIM + Style.BRIGHT + ':' + Style.RESET_ALL + Fore.CYAN + '%(lineno)d' |
| 82 | + tpl += Style.RESET_ALL |
| 83 | + |
| 84 | + return tpl |
| 85 | + |
| 86 | + |
| 87 | +class DebugLogFormatter(LogFormatter): |
| 88 | + |
| 89 | + """Provides greater technical details than standard log Formatter.""" |
| 90 | + |
| 91 | + template = debug_log_template |
0 commit comments