Skip to content

Instantly share code, notes, and snippets.

@jaredjenkins
Forked from brainsik/color_log.py
Created August 5, 2013 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredjenkins/6159932 to your computer and use it in GitHub Desktop.
Save jaredjenkins/6159932 to your computer and use it in GitHub Desktop.
import logging
from termcolor import colored
class ColorLog(object):
colormap = dict(
debug=dict(color='grey', attrs=['bold']),
info=dict(color='white'),
warn=dict(color='yellow', attrs=['bold']),
warning=dict(color='yellow', attrs=['bold']),
error=dict(color='red'),
critical=dict(color='red', attrs=['bold']),
)
def __init__(self, logger):
self._log = logger
def __getattr__(self, name):
if name in ['debug', 'info', 'warn', 'warning', 'error', 'critical']:
return lambda s, *args: getattr(self._log, name)(
colored(s, **self.colormap[name]), *args)
return getattr(self._log, name)
log = ColorLog(logging.getLogger(__name__))
if __name__ == '__main__':
log.setLevel(logging.DEBUG)
stdout = logging.StreamHandler()
stdout.setLevel(logging.DEBUG)
log.addHandler(stdout)
log.debug("booooring . . .")
log.info("pleasing anecdote")
log.warn("awkward utterance")
log.error("drunken rudeness")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment