|
@@ -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 |