Skip to content

Commit 407a416

Browse files
CLI stdout config file
1 parent 744fe91 commit 407a416

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/thread/config.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
from colorama import init, Fore, Style
3+
4+
init(autoreset = True)
5+
6+
7+
# Stdout color config #
8+
class ColorFormatter(logging.Formatter):
9+
COLORS = {
10+
'DEBUG' : Fore.BLUE,
11+
'INFO' : Fore.GREEN,
12+
'WARNING' : Fore.YELLOW,
13+
'ERROR' : Fore.RED,
14+
'CRITICAL': Fore.RED + Style.BRIGHT
15+
}
16+
17+
def format(self, record):
18+
color = self.COLORS.get(record.levelname, '')
19+
if color:
20+
record.levelname = color + Style.BRIGHT + f'{record.levelname:<9}|'
21+
record.msg = color + Fore.WHITE + Style.NORMAL + record.msg
22+
return logging.Formatter.format(self, record)
23+
24+
25+
class ColorLogger(logging.Logger):
26+
def __init__(self, name):
27+
logging.Logger.__init__(self, name, logging.DEBUG)
28+
color_formatter = ColorFormatter('%(levelname)s %(message)s' + Style.RESET_ALL)
29+
console = logging.StreamHandler()
30+
console.setFormatter(color_formatter)
31+
self.addHandler(console)

0 commit comments

Comments
 (0)