Skip to content

Commit

Permalink
Import README and __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Aug 26, 2011
0 parents commit c09d136
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Log colorizer

This file is a compilation of tricks found on the internet

Usefull as a submodule
56 changes: 56 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# This file is a compilation of tricks found on the internet
"""
Log colorizer
"""
import sys
import logging

from logging import StreamHandler

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
COLORS = {
'WARNING': YELLOW,
'INFO': GREEN,
'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):
"""Logging formatter adding console colors to the output.
"""
def format(self, record):
"""Format the record with colors."""
color = COLOR_SEQ % (30 + COLORS[record.levelname])
message = logging.Formatter.format(self, record)
message = message.replace('$RESET', RESET_SEQ)\
.replace('$BOLD', BOLD_SEQ)\
.replace('$COLOR', color)
for color, value in COLORS.items():
message = message.replace('$' + color, COLOR_SEQ % (value + 30))\
.replace('$BG' + color, COLOR_SEQ % (value + 40))\
.replace('$BG-' + color, COLOR_SEQ % (value + 40))
return message + RESET_SEQ


def make_colored_stream_handler(std=sys.stdout, level=logging.DEBUG):
"""Return a colored stream handler"""
handler = StreamHandler(std)
handler.setLevel(level)
handler.setFormatter(
ColorFormatter(
'$COLOR%(levelname)s %(asctime)s '
'$BOLD$COLOR%(name)s$RESET %(message)s'))
return handler

0 comments on commit c09d136

Please sign in to comment.