4
4
# From http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
5
5
6
6
import logging
7
+ import platform
8
+
9
+
7
10
# now we patch Python code to add color support to logging.StreamHandler
8
11
def add_coloring_to_emit_windows (fn ):
9
- # add methods we need to the class
12
+ # add methods we need to the class
10
13
def _out_handle (self ):
11
14
import ctypes
12
15
return ctypes .windll .kernel32 .GetStdHandle (self .STD_OUTPUT_HANDLE )
16
+
13
17
out_handle = property (_out_handle )
14
18
15
19
def _set_color (self , code ):
@@ -22,87 +26,85 @@ def _set_color(self, code):
22
26
setattr (logging .StreamHandler , '_set_color' , _set_color )
23
27
24
28
def new (* args ):
25
- FOREGROUND_BLUE = 0x0001 # text color contains blue.
26
- FOREGROUND_GREEN = 0x0002 # text color contains green.
27
- FOREGROUND_RED = 0x0004 # text color contains red.
28
- FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
29
- FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
29
+ FOREGROUND_BLUE = 0x0001 # text color contains blue.
30
+ FOREGROUND_GREEN = 0x0002 # text color contains green.
31
+ FOREGROUND_RED = 0x0004 # text color contains red.
32
+ FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
33
+ FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
30
34
# winbase.h
31
35
STD_INPUT_HANDLE = - 10
32
36
STD_OUTPUT_HANDLE = - 11
33
37
STD_ERROR_HANDLE = - 12
34
38
35
39
# wincon.h
36
- FOREGROUND_BLACK = 0x0000
37
- FOREGROUND_BLUE = 0x0001
38
- FOREGROUND_GREEN = 0x0002
39
- FOREGROUND_CYAN = 0x0003
40
- FOREGROUND_RED = 0x0004
41
- FOREGROUND_MAGENTA = 0x0005
42
- FOREGROUND_YELLOW = 0x0006
43
- FOREGROUND_GREY = 0x0007
44
- FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified.
45
-
46
- BACKGROUND_BLACK = 0x0000
47
- BACKGROUND_BLUE = 0x0010
48
- BACKGROUND_GREEN = 0x0020
49
- BACKGROUND_CYAN = 0x0030
50
- BACKGROUND_RED = 0x0040
51
- BACKGROUND_MAGENTA = 0x0050
52
- BACKGROUND_YELLOW = 0x0060
53
- BACKGROUND_GREY = 0x0070
54
- BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
40
+ FOREGROUND_BLACK = 0x0000
41
+ FOREGROUND_BLUE = 0x0001
42
+ FOREGROUND_GREEN = 0x0002
43
+ FOREGROUND_CYAN = 0x0003
44
+ FOREGROUND_RED = 0x0004
45
+ FOREGROUND_MAGENTA = 0x0005
46
+ FOREGROUND_YELLOW = 0x0006
47
+ FOREGROUND_GREY = 0x0007
48
+ FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified.
49
+
50
+ BACKGROUND_BLACK = 0x0000
51
+ BACKGROUND_BLUE = 0x0010
52
+ BACKGROUND_GREEN = 0x0020
53
+ BACKGROUND_CYAN = 0x0030
54
+ BACKGROUND_RED = 0x0040
55
+ BACKGROUND_MAGENTA = 0x0050
56
+ BACKGROUND_YELLOW = 0x0060
57
+ BACKGROUND_GREY = 0x0070
58
+ BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
55
59
56
60
levelno = args [1 ].levelno
57
- if ( levelno >= 50 ) :
58
- color = BACKGROUND_YELLOW | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY
59
- elif ( levelno >= 40 ) :
61
+ if levelno >= 50 :
62
+ color = BACKGROUND_YELLOW | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY
63
+ elif levelno >= 40 :
60
64
color = FOREGROUND_RED | FOREGROUND_INTENSITY
61
- elif ( levelno >= 30 ) :
65
+ elif levelno >= 30 :
62
66
color = FOREGROUND_YELLOW | FOREGROUND_INTENSITY
63
- elif ( levelno >= 20 ) :
67
+ elif levelno >= 20 :
64
68
color = FOREGROUND_GREEN
65
- elif ( levelno >= 10 ) :
69
+ elif levelno >= 10 :
66
70
color = FOREGROUND_MAGENTA
67
71
else :
68
- color = FOREGROUND_WHITE
72
+ color = FOREGROUND_WHITE
69
73
args [0 ]._set_color (color )
70
74
71
75
ret = fn (* args )
72
- args [0 ]._set_color ( FOREGROUND_WHITE )
73
- #print "after"
76
+ args [0 ]._set_color (FOREGROUND_WHITE )
74
77
return ret
78
+
75
79
return new
76
80
81
+
77
82
def add_coloring_to_emit_ansi (fn ):
78
83
# add methods we need to the class
79
84
def new (* args ):
80
85
levelno = args [1 ].levelno
81
- if ( levelno >= 50 ) :
82
- color = '\x1b [31m' # red
83
- elif ( levelno >= 40 ) :
84
- color = '\x1b [31m' # red
85
- elif ( levelno >= 30 ) :
86
- color = '\x1b [33m' # yellow
87
- elif ( levelno >= 20 ) :
88
- color = '\x1b [32m' # green
89
- elif ( levelno >= 10 ) :
90
- color = '\x1b [35m' # pink
86
+ if levelno >= 50 :
87
+ color = '\x1b [31m' # red
88
+ elif levelno >= 40 :
89
+ color = '\x1b [31m' # red
90
+ elif levelno >= 30 :
91
+ color = '\x1b [33m' # yellow
92
+ elif levelno >= 20 :
93
+ color = '\x1b [32m' # green
94
+ elif levelno >= 10 :
95
+ color = '\x1b [35m' # pink
91
96
else :
92
- color = '\x1b [0m' # normal
93
- args [1 ].msg = color + args [1 ].msg + '\x1b [0m' # normal
94
- #print "after"
97
+ color = '\x1b [0m' # normal
98
+ args [1 ].msg = color + args [1 ].msg + '\x1b [0m' # normal
95
99
return fn (* args )
100
+
96
101
return new
97
102
98
- import platform
99
- if platform .system ()== 'Windows' :
100
- # Windows does not support ANSI escapes and we are using API calls to set the console color
101
- logging .StreamHandler .emit = add_coloring_to_emit_windows (logging .StreamHandler .emit )
102
- else :
103
- # all non-Windows platforms are supporting ANSI escapes so we use them
104
- logging .StreamHandler .emit = add_coloring_to_emit_ansi (logging .StreamHandler .emit )
105
- #log = logging.getLogger()
106
- #log.addFilter(log_filter())
107
- #//hdlr = logging.StreamHandler()
108
- #//hdlr.setFormatter(formatter())
103
+
104
+ def init ():
105
+ if platform .system () == 'Windows' :
106
+ # Windows does not support ANSI escapes and we are using API calls to set the console color
107
+ logging .StreamHandler .emit = add_coloring_to_emit_windows (logging .StreamHandler .emit )
108
+ else :
109
+ # all non-Windows platforms are supporting ANSI escapes so we use them
110
+ logging .StreamHandler .emit = add_coloring_to_emit_ansi (logging .StreamHandler .emit )
0 commit comments