def __init__(self): logging.StreamHandler.__init__(self) if ON_WIN and HAS_PYREADLINE: self.console = Console() # Avoid printing colors if not a tty: if not sys.stdout.isatty(): for k in COLORS.keys(): COLORS[k] = "" self.console = None
class ColorLogHandler(logging.StreamHandler): """A class that outputs nice colored messages """ # Warning: # Most of the code from logging's formatters is re-written in # ColorHandler.emit(), so using setFormatter would # have no effect on color logger... def __init__(self): logging.StreamHandler.__init__(self) if ON_WIN and HAS_PYREADLINE: self.console = Console() # Avoid printing colors if not a tty: if not sys.stdout.isatty(): for k in COLORS.keys(): COLORS[k] = "" self.console = None def emit(self, record): """Override StreamHandler.emit method """ name = record.name level = record.levelname mess = record.getMessage() res = COLORS["bold"] if record.levelno == logging.DEBUG: res += COLORS["blue"] elif record.levelno == logging.INFO: res += COLORS["green"] elif record.levelno >= logging.WARNING: res += COLORS["red"] level = "[%s]" % level if record.levelno != logging.INFO: res += "%-12s" % level res += "%-12s " % name res += mess res += COLORS["clear"] res += "\n" if ON_WIN and HAS_PYREADLINE: if self.console is not None: self.console.write_color(res) else: sys.stdout.write(res) else: if record.levelno < logging.WARNING: sys.stdout.write(res) else: sys.stderr.write(res)
import sys import os import datetime import difflib import functools # Try using pyreadline so that we can # have colors on windows, too. _console = None HAS_PYREADLINE = True if os.name == 'nt': try: # pylint: disable-msg=F0401 from pyreadline.console import Console _console = Console() except: HAS_PYREADLINE = False # ANSI color codes, as classes, # so that we can use :: # # qisys.ui.msg(qisys.ui.bold, "This is bold", qisys.ui.reset) class _Color: def __init__(self, code, modifier=None): self.code = '\033[%d' % code if modifier is not None: self.code += ';%dm' % modifier else: