From 38f4767297440437512886f7b265f37c9b055384 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Fri, 14 Jan 2011 17:45:35 +0530 Subject: [PATCH] Added colors to logging --- hyde/engine.py | 9 ++------ hyde/server.py | 7 ++---- hyde/util.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/hyde/engine.py b/hyde/engine.py index 7ba9eb4..98005d1 100644 --- a/hyde/engine.py +++ b/hyde/engine.py @@ -9,19 +9,14 @@ from hyde.layout import Layout, HYDE_DATA from hyde.model import Config from hyde.site import Site from hyde.version import __version__ +from hyde.util import getLoggerWithConsoleHandler -import logging import os import yaml HYDE_LAYOUTS = "HYDE_LAYOUTS" -logger = logging.getLogger('hyde.engine') -logger.setLevel(logging.DEBUG) - -import sys -logger.addHandler(logging.StreamHandler(sys.stdout)) - +logger = getLoggerWithConsoleHandler('hyde.engine') class Engine(Application): """ diff --git a/hyde/server.py b/hyde/server.py index b850835..0ecbde0 100644 --- a/hyde/server.py +++ b/hyde/server.py @@ -10,12 +10,9 @@ from BaseHTTPServer import HTTPServer from hyde.fs import File, Folder from hyde.site import Site from hyde.generator import Generator +from hyde.util import getLoggerWithConsoleHandler -import logging -logger = logging.getLogger('hyde.server') - -import sys -logger.addHandler(logging.StreamHandler(sys.stdout)) +logger = getLoggerWithConsoleHandler('hyde.engine') class HydeRequestHandler(SimpleHTTPRequestHandler): """ diff --git a/hyde/util.py b/hyde/util.py index f42c4b3..d9da797 100644 --- a/hyde/util.py +++ b/hyde/util.py @@ -2,6 +2,7 @@ Module for python 2.6 compatibility. """ import logging +import sys try: from logging import NullHandler @@ -16,6 +17,19 @@ except: """ pass +def getLoggerWithConsoleHandler(logger_name): + logger = logging.getLogger('hyde.server') + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler(sys.stdout) + formatter = ColorFormatter(fmt="$COLOR%(levelname)s " + "$RESET %(asctime)s " + "$BOLD$COLOR%(name)s$RESET " + "%(message)s", datefmt='%H:%M:%S') + handler.setFormatter(formatter) + logger.addHandler(handler) + return logger + + def getLoggerWithNullHandler(logger_name): """ Gets the logger initialized with the `logger_name` @@ -24,3 +38,49 @@ def getLoggerWithNullHandler(logger_name): logger = logging.getLogger(logger_name) logger.addHandler(NullHandler()) return logger + + +## Code stolen from : +## http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored/2532931#2532931 +## +BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) + +COLORS = { + 'WARNING' : YELLOW, + 'INFO' : WHITE, + 'DEBUG' : BLUE, + 'CRITICAL' : YELLOW, + 'ERROR' : RED, + 'RED' : RED, + 'GREEN' : GREEN, + 'YELLOW' : YELLOW, + 'BLUE' : BLUE, + 'MAGENTA' : MAGENTA, + 'CYAN' : CYAN, + 'WHITE' : WHITE, +} + +RESET_SEQ = "\033[0m" +COLOR_SEQ = "\033[1;%dm" +BOLD_SEQ = "\033[1m" + +class ColorFormatter(logging.Formatter): + + def __init__(self, *args, **kwargs): + # can't do super(...) here because Formatter is an old school class + logging.Formatter.__init__(self, *args, **kwargs) + + def format(self, record): + levelname = record.levelname + color = COLOR_SEQ % (30 + COLORS[levelname]) + message = logging.Formatter.format(self, record) + message = message.replace("$RESET", RESET_SEQ)\ + .replace("$BOLD", BOLD_SEQ)\ + .replace("$COLOR", color) + for k,v in COLORS.items(): + message = message.replace("$" + k, COLOR_SEQ % (v+30))\ + .replace("$BG" + k, COLOR_SEQ % (v+40))\ + .replace("$BG-" + k, COLOR_SEQ % (v+40)) + return message + RESET_SEQ + +logging.ColorFormatter = ColorFormatter