Show More
@@ -1,141 +1,38 b'' | |||||
1 | #import logging |
|
|||
2 | ## now we patch Python code to add color support to logging.StreamHandler |
|
|||
3 | #def add_coloring_to_emit_windows(fn): |
|
|||
4 | # # add methods we need to the class |
|
|||
5 | # def _out_handle(self): |
|
|||
6 | # import ctypes |
|
|||
7 | # return ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE) |
|
|||
8 | # out_handle = property(_out_handle) |
|
|||
9 | # |
|
|||
10 | # def _set_color(self, code): |
|
|||
11 | # import ctypes |
|
|||
12 | # # Constants from the Windows API |
|
|||
13 | # self.STD_OUTPUT_HANDLE = -11 |
|
|||
14 | # hdl = ctypes.windll.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE) |
|
|||
15 | # ctypes.windll.kernel32.SetConsoleTextAttribute(hdl, code) |
|
|||
16 | # |
|
|||
17 | # setattr(logging.StreamHandler, '_set_color', _set_color) |
|
|||
18 | # |
|
|||
19 | # def new(*args): |
|
|||
20 | # FOREGROUND_BLUE = 0x0001 # text color contains blue. |
|
|||
21 | # FOREGROUND_GREEN = 0x0002 # text color contains green. |
|
|||
22 | # FOREGROUND_RED = 0x0004 # text color contains red. |
|
|||
23 | # FOREGROUND_INTENSITY = 0x0008 # text color is intensified. |
|
|||
24 | # FOREGROUND_WHITE = FOREGROUND_BLUE|FOREGROUND_GREEN |FOREGROUND_RED |
|
|||
25 | # # winbase.h |
|
|||
26 | # STD_INPUT_HANDLE = -10 |
|
|||
27 | # STD_OUTPUT_HANDLE = -11 |
|
|||
28 | # STD_ERROR_HANDLE = -12 |
|
|||
29 | # |
|
|||
30 | # # wincon.h |
|
|||
31 | # FOREGROUND_BLACK = 0x0000 |
|
|||
32 | # FOREGROUND_BLUE = 0x0001 |
|
|||
33 | # FOREGROUND_GREEN = 0x0002 |
|
|||
34 | # FOREGROUND_CYAN = 0x0003 |
|
|||
35 | # FOREGROUND_RED = 0x0004 |
|
|||
36 | # FOREGROUND_MAGENTA = 0x0005 |
|
|||
37 | # FOREGROUND_YELLOW = 0x0006 |
|
|||
38 | # FOREGROUND_GREY = 0x0007 |
|
|||
39 | # FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified. |
|
|||
40 | # |
|
|||
41 | # BACKGROUND_BLACK = 0x0000 |
|
|||
42 | # BACKGROUND_BLUE = 0x0010 |
|
|||
43 | # BACKGROUND_GREEN = 0x0020 |
|
|||
44 | # BACKGROUND_CYAN = 0x0030 |
|
|||
45 | # BACKGROUND_RED = 0x0040 |
|
|||
46 | # BACKGROUND_MAGENTA = 0x0050 |
|
|||
47 | # BACKGROUND_YELLOW = 0x0060 |
|
|||
48 | # BACKGROUND_GREY = 0x0070 |
|
|||
49 | # BACKGROUND_INTENSITY = 0x0080 # background color is intensified. |
|
|||
50 | # |
|
|||
51 | # levelno = args[1].levelno |
|
|||
52 | # if(levelno>=50): |
|
|||
53 | # color = BACKGROUND_YELLOW | FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY |
|
|||
54 | # elif(levelno>=40): |
|
|||
55 | # color = FOREGROUND_RED | FOREGROUND_INTENSITY |
|
|||
56 | # elif(levelno>=30): |
|
|||
57 | # color = FOREGROUND_YELLOW | FOREGROUND_INTENSITY |
|
|||
58 | # elif(levelno>=20): |
|
|||
59 | # color = FOREGROUND_GREEN |
|
|||
60 | # elif(levelno>=10): |
|
|||
61 | # color = FOREGROUND_MAGENTA |
|
|||
62 | # else: |
|
|||
63 | # color = FOREGROUND_WHITE |
|
|||
64 | # args[0]._set_color(color) |
|
|||
65 | # |
|
|||
66 | # ret = fn(*args) |
|
|||
67 | # args[0]._set_color( FOREGROUND_WHITE ) |
|
|||
68 | # #print "after" |
|
|||
69 | # return ret |
|
|||
70 | # return new |
|
|||
71 | # |
|
|||
72 | #def add_coloring_to_emit_ansi(fn): |
|
|||
73 | # # add methods we need to the class |
|
|||
74 | # def new(*args): |
|
|||
75 | # levelno = args[1].levelno |
|
|||
76 | # if(levelno>=50): |
|
|||
77 | # color = '\x1b[31m' # red |
|
|||
78 | # elif(levelno>=40): |
|
|||
79 | # color = '\x1b[31m' # red |
|
|||
80 | # elif(levelno>=30): |
|
|||
81 | # color = '\x1b[33m' # yellow |
|
|||
82 | # elif(levelno>=20): |
|
|||
83 | # color = '\x1b[32m' # green |
|
|||
84 | # elif(levelno>=10): |
|
|||
85 | # color = '\x1b[35m' # pink |
|
|||
86 | # else: |
|
|||
87 | # color = '\x1b[0m' # normal |
|
|||
88 | # args[1].msg = color + args[1].msg + '\x1b[0m' # normal |
|
|||
89 | # #print "after" |
|
|||
90 | # return fn(*args) |
|
|||
91 | # return new |
|
|||
92 | # |
|
|||
93 | #import platform |
|
|||
94 | #if platform.system()=='Windows': |
|
|||
95 | # # Windows does not support ANSI escapes and we are using API calls to set the console color |
|
|||
96 | # logging.StreamHandler.emit = add_coloring_to_emit_windows(logging.StreamHandler.emit) |
|
|||
97 | #else: |
|
|||
98 | # # all non-Windows platforms are supporting ANSI escapes so we use them |
|
|||
99 | # logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit) |
|
|||
100 | # #log = logging.getLogger() |
|
|||
101 | # #log.addFilter(log_filter()) |
|
|||
102 | # #//hdlr = logging.StreamHandler() |
|
|||
103 | # #//hdlr.setFormatter(formatter()) |
|
|||
104 |
|
|
1 | ||
105 | import logging |
|
2 | import logging | |
106 |
|
3 | |||
107 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) |
|
4 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |
108 |
|
5 | |||
109 | # Sequences |
|
6 | # Sequences | |
110 | RESET_SEQ = "\033[0m" |
|
7 | RESET_SEQ = "\033[0m" | |
111 | COLOR_SEQ = "\033[1;%dm" |
|
8 | COLOR_SEQ = "\033[1;%dm" | |
112 | BOLD_SEQ = "\033[1m" |
|
9 | BOLD_SEQ = "\033[1m" | |
113 |
|
10 | |||
114 | COLORS = { |
|
11 | COLORS = { | |
115 | 'CRITICAL': MAGENTA, # level 50 |
|
12 | 'CRITICAL': MAGENTA, # level 50 | |
116 | 'ERROR': RED, # level 40 |
|
13 | 'ERROR': RED, # level 40 | |
117 | 'WARNING': CYAN, # level 30 |
|
14 | 'WARNING': CYAN, # level 30 | |
118 | 'INFO': GREEN, # level 20 |
|
15 | 'INFO': GREEN, # level 20 | |
119 | 'DEBUG': BLUE, # level 10 |
|
16 | 'DEBUG': BLUE, # level 10 | |
120 | } |
|
17 | } | |
121 |
|
18 | |||
122 | class ColorFormatter(logging.Formatter): |
|
19 | class ColorFormatter(logging.Formatter): | |
123 |
|
20 | |||
124 | def __init__(self, *args, **kwargs): |
|
21 | def __init__(self, *args, **kwargs): | |
125 | # can't do super(...) here because Formatter is an old school class |
|
22 | # can't do super(...) here because Formatter is an old school class | |
126 | logging.Formatter.__init__(self, *args, **kwargs) |
|
23 | logging.Formatter.__init__(self, *args, **kwargs) | |
127 |
|
24 | |||
128 | def format(self, record): |
|
25 | def format(self, record): | |
129 | """ |
|
26 | """ | |
130 | Changes record's levelname to use with COLORS enum |
|
27 | Changes record's levelname to use with COLORS enum | |
131 | """ |
|
28 | """ | |
132 |
|
29 | |||
133 | levelname = record.levelname |
|
30 | levelname = record.levelname | |
134 | start = COLOR_SEQ % (COLORS[levelname]) |
|
31 | start = COLOR_SEQ % (COLORS[levelname]) | |
135 | def_record = logging.Formatter.format(self, record) |
|
32 | def_record = logging.Formatter.format(self, record) | |
136 | end = RESET_SEQ |
|
33 | end = RESET_SEQ | |
137 |
|
34 | |||
138 | colored_record = start + def_record + end |
|
35 | colored_record = start + def_record + end | |
139 | return colored_record |
|
36 | return colored_record | |
140 |
|
37 | |||
141 | logging.ColorFormatter = ColorFormatter |
|
38 | logging.ColorFormatter = ColorFormatter |
General Comments 0
You need to be logged in to leave comments.
Login now