Show More
@@ -1,85 +1,85 b'' | |||
|
1 | 1 | |
|
2 | 2 | import logging |
|
3 | 3 | |
|
4 | 4 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) |
|
5 | 5 | |
|
6 | 6 | # Sequences |
|
7 | 7 | RESET_SEQ = "\033[0m" |
|
8 |
COLOR_SEQ = "\033[ |
|
|
8 | COLOR_SEQ = "\033[0;%dm" | |
|
9 | 9 | BOLD_SEQ = "\033[1m" |
|
10 | 10 | |
|
11 | 11 | COLORS = { |
|
12 | 12 | 'CRITICAL': MAGENTA, |
|
13 | 13 | 'ERROR': RED, |
|
14 | 14 | 'WARNING': CYAN, |
|
15 | 15 | 'INFO': GREEN, |
|
16 | 16 | 'DEBUG': BLUE, |
|
17 | 17 | 'SQL': YELLOW |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | def one_space_trim(s): |
|
22 | 22 | if s.find(" ") == -1: |
|
23 | 23 | return s |
|
24 | 24 | else: |
|
25 | 25 | s = s.replace(' ', ' ') |
|
26 | 26 | return one_space_trim(s) |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | def format_sql(sql): |
|
30 | 30 | sql = sql.replace('\n', '') |
|
31 | 31 | sql = one_space_trim(sql) |
|
32 | 32 | sql = sql\ |
|
33 | 33 | .replace(',', ',\n\t')\ |
|
34 | 34 | .replace('SELECT', '\n\tSELECT \n\t')\ |
|
35 | 35 | .replace('UPDATE', '\n\tUPDATE \n\t')\ |
|
36 | 36 | .replace('DELETE', '\n\tDELETE \n\t')\ |
|
37 | 37 | .replace('FROM', '\n\tFROM')\ |
|
38 | 38 | .replace('ORDER BY', '\n\tORDER BY')\ |
|
39 | 39 | .replace('LIMIT', '\n\tLIMIT')\ |
|
40 | 40 | .replace('WHERE', '\n\tWHERE')\ |
|
41 | 41 | .replace('AND', '\n\tAND')\ |
|
42 | 42 | .replace('LEFT', '\n\tLEFT')\ |
|
43 | 43 | .replace('INNER', '\n\tINNER')\ |
|
44 | 44 | .replace('INSERT', '\n\tINSERT')\ |
|
45 | 45 | .replace('DELETE', '\n\tDELETE') |
|
46 | 46 | return sql |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | class ColorFormatter(logging.Formatter): |
|
50 | 50 | |
|
51 | 51 | def __init__(self, *args, **kwargs): |
|
52 | 52 | # can't do super(...) here because Formatter is an old school class |
|
53 | 53 | logging.Formatter.__init__(self, *args, **kwargs) |
|
54 | 54 | |
|
55 | 55 | def format(self, record): |
|
56 | 56 | """ |
|
57 | 57 | Changes record's levelname to use with COLORS enum |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | 60 | levelname = record.levelname |
|
61 | 61 | start = COLOR_SEQ % (COLORS[levelname]) |
|
62 | 62 | def_record = logging.Formatter.format(self, record) |
|
63 | 63 | end = RESET_SEQ |
|
64 | 64 | |
|
65 | 65 | colored_record = ''.join([start, def_record, end]) |
|
66 | 66 | return colored_record |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | class ColorFormatterSql(logging.Formatter): |
|
70 | 70 | |
|
71 | 71 | def __init__(self, *args, **kwargs): |
|
72 | 72 | # can't do super(...) here because Formatter is an old school class |
|
73 | 73 | logging.Formatter.__init__(self, *args, **kwargs) |
|
74 | 74 | |
|
75 | 75 | def format(self, record): |
|
76 | 76 | """ |
|
77 | 77 | Changes record's levelname to use with COLORS enum |
|
78 | 78 | """ |
|
79 | 79 | |
|
80 | 80 | start = COLOR_SEQ % (COLORS['SQL']) |
|
81 | 81 | def_record = format_sql(logging.Formatter.format(self, record)) |
|
82 | 82 | end = RESET_SEQ |
|
83 | 83 | |
|
84 | 84 | colored_record = ''.join([start, def_record, end]) |
|
85 | 85 | return colored_record |
General Comments 0
You need to be logged in to leave comments.
Login now