-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c09d136
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |