Skip to content

Commit 340aaef

Browse files
authored
Merge pull request #5 from desultory/dev
make a colorize module, refactor colored logging to use it
2 parents c42b215 + 8721607 commit 340aaef

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

src/zenlib/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from .logging import ColorLognameFormatter, loggify
22
from .types import NoDupFlatList, validatedDataclass
3-
from .util import check_dict, handle_plural, pretty_print, replace_file_line, update_init, walk_dict
3+
from .util import colorize, check_dict, handle_plural, pretty_print, replace_file_line, update_init, walk_dict
44

55
__all__ = [
6+
"colorize",
67
"ColorLognameFormatter",
78
"loggify",
89
"handle_plural",
Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
11
__author__ = "desultory"
2-
__version__ = "2.0.0"
3-
2+
__version__ = "3.0.0"
43

54
from logging import Formatter
65

6+
from zenlib.util import colorize
77

8-
class ColorLognameFormatter(Formatter):
9-
"""
10-
ColorLognameFormatter Class
11-
Add the handler to the stdout handler using:
12-
stdout_handler = logging.StreamHandler()
13-
stdout_handler.setFormatter(ColorLognameFormatter())
14-
"""
15-
16-
# Define the color codes
17-
_reset_str = '\x1b[0m'
18-
_grey_str = '\x1b[37m'
19-
_blue_str = '\x1b[34m'
20-
_yllw_str = '\x1b[33m'
21-
_sred_str = '\x1b[31m'
22-
_bred_str = '\x1b[31;1m'
23-
_magenta_str = '\x1b[35m'
24-
# Format into a dict
25-
_color_levelname = {'DEBUG': _grey_str,
26-
'INFO': _blue_str,
27-
'WARNING': _yllw_str,
28-
'ERROR': _sred_str,
29-
'CRITICAL': _bred_str}
308

31-
def __init__(self, fmt='%(levelname)s | %(message)s', *args, **kwargs):
9+
class ColorLognameFormatter(Formatter):
10+
"""A logging formatter which colors the levelname of the log message.
11+
Uses the zenlib.util.colorize function to color the levelname.
12+
Normal levelnames are padded to the length of the longest levelname."""
13+
14+
level_colors = {
15+
"DEBUG": {"color": "white"},
16+
"INFO": {"color": "blue"},
17+
"WARNING": {"color": "yellow"},
18+
"ERROR": {"color": "red", "bold": True},
19+
"CRITICAL": {"color": "red", "bold": True, "bright": True},
20+
}
21+
22+
def __init__(self, fmt="%(levelname)s | %(message)s", *args, **kwargs):
3223
super().__init__(fmt, *args, **kwargs)
33-
self._level_str_len = max([len(name) for name in self._color_levelname])
34-
35-
def color_levelname(self, levelname):
36-
""" Adds color to a levelname string """
37-
color = self._color_levelname.get(levelname, self._magenta_str)
38-
return f"{color}{levelname}{self._reset_str}".ljust(self._level_str_len + len(color) + len(self._reset_str), ' ')
24+
self.level_str_width = max(len(name) for name in self.level_colors) - 1
3925

4026
def format(self, record):
4127
# When calling format, replace the levelname with a colored version
4228
# Note: the string size is greatly increased because of the color codes
4329
old_levelname = record.levelname
44-
record.levelname = self.color_levelname(record.levelname)
30+
color_info = self.level_colors.get(record.levelname, {"color": "white"})
31+
record.levelname = colorize(record.levelname.ljust(self.level_str_width), **color_info)
4532
format_str = super().format(record)
4633
record.levelname = old_levelname
4734
return format_str

src/zenlib/util/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .dict_check import contains, unset
2+
from .colorize import colorize
23
from .handle_plural import handle_plural
34
from .main_funcs import get_args_n_logger, get_kwargs, get_kwargs_from_args, init_argparser, init_logger, process_args
45
from .merge_class import merge_class
@@ -7,6 +8,7 @@
78

89
__all__ = [
910
"handle_plural",
11+
"colorize",
1012
"NoDupFlatList",
1113
"pretty_print",
1214
"replace_file_line",

src/zenlib/util/colorize.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
__version__ = "0.1.0"
2+
3+
from enum import Enum
4+
5+
6+
ANSI_START = "\x1b["
7+
8+
class Colors(Enum):
9+
RED = 31
10+
GREEN = 32
11+
YELLOW = 33
12+
BLUE = 34
13+
MAGENTA = 35
14+
CYAN = 36
15+
WHITE = 37
16+
17+
18+
class ANSICode:
19+
def __init__(self, code, bright=False, bold=False):
20+
self.code = code
21+
self.bright = bright
22+
self.bold = bold
23+
24+
def __str__(self):
25+
color_code = self.code
26+
color_code += 60 if self.bright else 0
27+
bold_str = ";1" if self.bold else ""
28+
return f"{ANSI_START}{color_code}{bold_str}m"
29+
30+
31+
def colorize(text: str, color: str, bright=False, bold=False) -> str:
32+
color_code = Colors[color.upper()].value
33+
return f"{ANSICode(color_code, bright, bold)}{text}{ANSICode(0)}"

0 commit comments

Comments
 (0)