"""Nicer log formatting with colours. Code copied from Tornado, Apache licensed. """ # Copyright 2012 Facebook # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import logging import sys try: import curses except ImportError: curses = None def _stderr_supports_color(): color = False if curses and hasattr(sys.stderr, 'isatty') and sys.stderr.isatty(): try: curses.setupterm() if curses.tigetnum("colors") > 0: color = True except Exception: pass return color class LogFormatter(logging.Formatter): """Log formatter with colour support """ DEFAULT_COLORS = { logging.INFO: 2, # Green logging.WARNING: 3, # Yellow logging.ERROR: 1, # Red logging.CRITICAL: 1, } def __init__(self, color=True, datefmt=None): r""" :arg bool color: Enables color support. :arg string fmt: Log message format. It will be applied to the attributes dict of log records. The text between ``%(color)s`` and ``%(end_color)s`` will be colored depending on the level if color support is on. :arg dict colors: color mappings from logging level to terminal color code :arg string datefmt: Datetime format. Used for formatting ``(asctime)`` placeholder in ``prefix_fmt``. .. versionchanged:: 3.2 Added ``fmt`` and ``datefmt`` arguments. """ logging.Formatter.__init__(self, datefmt=datefmt) self._colors = {} if color and _stderr_supports_color(): # The curses module has some str/bytes confusion in # python3. Until version 3.2.3, most methods return # bytes, but only accept strings. In addition, we want to # output these strings with the logging module, which # works with unicode strings. The explicit calls to # unicode() below are harmless in python2 but will do the # right conversion in python 3. fg_color = (curses.tigetstr("setaf") or curses.tigetstr("setf") or "") if (3, 0) < sys.version_info < (3, 2, 3): fg_color = str(fg_color, "ascii") for levelno, code in self.DEFAULT_COLORS.items(): self._colors[levelno] = str( curses.tparm(fg_color, code), "ascii") self._normal = str(curses.tigetstr("sgr0"), "ascii") scr = curses.initscr() self.termwidth = scr.getmaxyx()[1] curses.endwin() else: self._normal = '' # Default width is usually 80, but too wide is # worse than too narrow self.termwidth = 70 def formatMessage(self, record): mlen = len(record.message) right_text = '{initial}-{name}'.format(initial=record.levelname[0], name=record.name) if mlen + len(right_text) < self.termwidth: space = ' ' * (self.termwidth - (mlen + len(right_text))) else: space = ' ' if record.levelno in self._colors: start_color = self._colors[record.levelno] end_color = self._normal else: start_color = end_color = '' return record.message + space + start_color + right_text + end_color def enable_colourful_output(level=logging.INFO): handler = logging.StreamHandler() handler.setFormatter(LogFormatter()) logging.root.addHandler(handler) logging.root.setLevel(level)
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
.__init__.pyo.40009 | File | 274 B | 0644 |
|
._in_process.pyo.40009 | File | 10.68 KB | 0644 |
|
.build.pyo.40009 | File | 4.31 KB | 0644 |
|
.check.pyo.40009 | File | 5.9 KB | 0644 |
|
.colorlog.pyo.40009 | File | 3.62 KB | 0644 |
|
.compat.pyo.40009 | File | 1.44 KB | 0644 |
|
.dirtools.pyo.40009 | File | 1.72 KB | 0644 |
|
.envbuild.pyo.40009 | File | 5.4 KB | 0644 |
|
.meta.pyo.40009 | File | 3.54 KB | 0644 |
|
.wrappers.pyo.40009 | File | 12.72 KB | 0644 |
|
__init__.py | File | 84 B | 0644 |
|
__init__.pyc | File | 274 B | 0644 |
|
__init__.pyo | File | 274 B | 0644 |
|
_in_process.py | File | 8.24 KB | 0644 |
|
_in_process.pyc | File | 10.68 KB | 0644 |
|
_in_process.pyo | File | 10.68 KB | 0644 |
|
build.py | File | 3.26 KB | 0644 |
|
build.pyc | File | 4.31 KB | 0644 |
|
build.pyo | File | 4.31 KB | 0644 |
|
check.py | File | 5.82 KB | 0644 |
|
check.pyc | File | 5.9 KB | 0644 |
|
check.pyo | File | 5.9 KB | 0644 |
|
colorlog.py | File | 4 KB | 0644 |
|
colorlog.pyc | File | 3.62 KB | 0644 |
|
colorlog.pyo | File | 3.62 KB | 0644 |
|
compat.py | File | 780 B | 0644 |
|
compat.pyc | File | 1.44 KB | 0644 |
|
compat.pyo | File | 1.44 KB | 0644 |
|
dirtools.py | File | 1.1 KB | 0644 |
|
dirtools.pyc | File | 1.72 KB | 0644 |
|
dirtools.pyo | File | 1.72 KB | 0644 |
|
envbuild.py | File | 5.9 KB | 0644 |
|
envbuild.pyc | File | 5.4 KB | 0644 |
|
envbuild.pyo | File | 5.4 KB | 0644 |
|
meta.py | File | 2.41 KB | 0644 |
|
meta.pyc | File | 3.54 KB | 0644 |
|
meta.pyo | File | 3.54 KB | 0644 |
|
wrappers.py | File | 10.53 KB | 0644 |
|
wrappers.pyc | File | 12.72 KB | 0644 |
|
wrappers.pyo | File | 12.72 KB | 0644 |
|