Browse Source

Added colors to logging

main
Lakshmi Vyasarajan 14 years ago
parent
commit
38f4767297
3 changed files with 64 additions and 12 deletions
  1. +2
    -7
      hyde/engine.py
  2. +2
    -5
      hyde/server.py
  3. +60
    -0
      hyde/util.py

+ 2
- 7
hyde/engine.py View File

@@ -9,19 +9,14 @@ from hyde.layout import Layout, HYDE_DATA
from hyde.model import Config from hyde.model import Config
from hyde.site import Site from hyde.site import Site
from hyde.version import __version__ from hyde.version import __version__
from hyde.util import getLoggerWithConsoleHandler


import logging
import os import os
import yaml import yaml


HYDE_LAYOUTS = "HYDE_LAYOUTS" 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): class Engine(Application):
""" """


+ 2
- 5
hyde/server.py View File

@@ -10,12 +10,9 @@ from BaseHTTPServer import HTTPServer
from hyde.fs import File, Folder from hyde.fs import File, Folder
from hyde.site import Site from hyde.site import Site
from hyde.generator import Generator 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): class HydeRequestHandler(SimpleHTTPRequestHandler):
""" """


+ 60
- 0
hyde/util.py View File

@@ -2,6 +2,7 @@
Module for python 2.6 compatibility. Module for python 2.6 compatibility.
""" """
import logging import logging
import sys


try: try:
from logging import NullHandler from logging import NullHandler
@@ -16,6 +17,19 @@ except:
""" """
pass 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): def getLoggerWithNullHandler(logger_name):
""" """
Gets the logger initialized with the `logger_name` Gets the logger initialized with the `logger_name`
@@ -24,3 +38,49 @@ def getLoggerWithNullHandler(logger_name):
logger = logging.getLogger(logger_name) logger = logging.getLogger(logger_name)
logger.addHandler(NullHandler()) logger.addHandler(NullHandler())
return logger 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

Loading…
Cancel
Save