File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments