Show More
@@ -1,170 +1,171 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2018 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import sys |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) |
|
26 | 26 | |
|
27 | 27 | # Sequences |
|
28 | 28 | RESET_SEQ = "\033[0m" |
|
29 | 29 | COLOR_SEQ = "\033[0;%dm" |
|
30 | 30 | BOLD_SEQ = "\033[1m" |
|
31 | 31 | |
|
32 | 32 | COLORS = { |
|
33 | 33 | 'CRITICAL': MAGENTA, |
|
34 | 34 | 'ERROR': RED, |
|
35 | 35 | 'WARNING': CYAN, |
|
36 | 36 | 'INFO': GREEN, |
|
37 | 37 | 'DEBUG': BLUE, |
|
38 | 38 | 'SQL': YELLOW |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def one_space_trim(s): |
|
43 | 43 | if s.find(" ") == -1: |
|
44 | 44 | return s |
|
45 | 45 | else: |
|
46 | 46 | s = s.replace(' ', ' ') |
|
47 | 47 | return one_space_trim(s) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | def format_sql(sql): |
|
51 | 51 | sql = sql.replace('\n', '') |
|
52 | 52 | sql = one_space_trim(sql) |
|
53 | 53 | sql = sql\ |
|
54 | 54 | .replace(',', ',\n\t')\ |
|
55 | 55 | .replace('SELECT', '\n\tSELECT \n\t')\ |
|
56 | 56 | .replace('UPDATE', '\n\tUPDATE \n\t')\ |
|
57 | 57 | .replace('DELETE', '\n\tDELETE \n\t')\ |
|
58 | 58 | .replace('FROM', '\n\tFROM')\ |
|
59 | 59 | .replace('ORDER BY', '\n\tORDER BY')\ |
|
60 | 60 | .replace('LIMIT', '\n\tLIMIT')\ |
|
61 | 61 | .replace('WHERE', '\n\tWHERE')\ |
|
62 | 62 | .replace('AND', '\n\tAND')\ |
|
63 | 63 | .replace('LEFT', '\n\tLEFT')\ |
|
64 | 64 | .replace('INNER', '\n\tINNER')\ |
|
65 | 65 | .replace('INSERT', '\n\tINSERT')\ |
|
66 | 66 | .replace('DELETE', '\n\tDELETE') |
|
67 | 67 | return sql |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | class ExceptionAwareFormatter(logging.Formatter): |
|
71 | 71 | """ |
|
72 | 72 | Extended logging formatter which prints out remote tracebacks. |
|
73 | 73 | """ |
|
74 | 74 | |
|
75 | 75 | def formatException(self, ei): |
|
76 | 76 | ex_type, ex_value, ex_tb = ei |
|
77 | 77 | |
|
78 | 78 | local_tb = logging.Formatter.formatException(self, ei) |
|
79 | 79 | if hasattr(ex_value, '_vcs_server_traceback'): |
|
80 | 80 | |
|
81 | 81 | def formatRemoteTraceback(remote_tb_lines): |
|
82 | 82 | result = ["\n +--- This exception occured remotely on VCSServer - Remote traceback:\n\n"] |
|
83 | 83 | result.append(remote_tb_lines) |
|
84 | 84 | result.append("\n +--- End of remote traceback\n") |
|
85 | 85 | return result |
|
86 | 86 | |
|
87 | 87 | try: |
|
88 | 88 | if ex_type is not None and ex_value is None and ex_tb is None: |
|
89 | 89 | # possible old (3.x) call syntax where caller is only |
|
90 | 90 | # providing exception object |
|
91 | 91 | if type(ex_type) is not type: |
|
92 | 92 | raise TypeError( |
|
93 | 93 | "invalid argument: ex_type should be an exception " |
|
94 | 94 | "type, or just supply no arguments at all") |
|
95 | 95 | if ex_type is None and ex_tb is None: |
|
96 | 96 | ex_type, ex_value, ex_tb = sys.exc_info() |
|
97 | 97 | |
|
98 | 98 | remote_tb = getattr(ex_value, "_vcs_server_traceback", None) |
|
99 | 99 | |
|
100 | 100 | if remote_tb: |
|
101 | 101 | remote_tb = formatRemoteTraceback(remote_tb) |
|
102 | 102 | return local_tb + ''.join(remote_tb) |
|
103 | 103 | finally: |
|
104 | 104 | # clean up cycle to traceback, to allow proper GC |
|
105 | 105 | del ex_type, ex_value, ex_tb |
|
106 | 106 | |
|
107 | 107 | return local_tb |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | class ColorFormatter(ExceptionAwareFormatter): |
|
111 | 111 | |
|
112 | 112 | def format(self, record): |
|
113 | 113 | """ |
|
114 | 114 | Changes record's levelname to use with COLORS enum |
|
115 | 115 | """ |
|
116 | 116 | |
|
117 | 117 | levelname = record.levelname |
|
118 | 118 | start = COLOR_SEQ % (COLORS[levelname]) |
|
119 | 119 | def_record = logging.Formatter.format(self, record) |
|
120 | 120 | end = RESET_SEQ |
|
121 | 121 | |
|
122 | 122 | colored_record = ''.join([start, def_record, end]) |
|
123 | 123 | return colored_record |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | def _inject_req_id(record): |
|
127 | 127 | from pyramid.threadlocal import get_current_request |
|
128 | 128 | req = get_current_request() |
|
129 | req_id = 'req_id:%-36s ' % (getattr(req, 'req_id', None)) | |
|
129 | dummy = '00000000-0000-0000-0000-000000000000' | |
|
130 | req_id = 'req_id:%-36s' % (getattr(req, 'req_id', dummy)) | |
|
130 | 131 | record.req_id = req_id |
|
131 | 132 | |
|
132 | 133 | |
|
133 | 134 | class RequestTrackingFormatter(ExceptionAwareFormatter): |
|
134 | 135 | def format(self, record): |
|
135 | 136 | _inject_req_id(record) |
|
136 | 137 | def_record = logging.Formatter.format(self, record) |
|
137 | 138 | return def_record |
|
138 | 139 | |
|
139 | 140 | |
|
140 | 141 | class ColorRequestTrackingFormatter(ColorFormatter): |
|
141 | 142 | def format(self, record): |
|
142 | 143 | """ |
|
143 | 144 | Changes record's levelname to use with COLORS enum |
|
144 | 145 | """ |
|
145 | 146 | _inject_req_id(record) |
|
146 | 147 | levelname = record.levelname |
|
147 | 148 | start = COLOR_SEQ % (COLORS[levelname]) |
|
148 | 149 | def_record = logging.Formatter.format(self, record) |
|
149 | 150 | end = RESET_SEQ |
|
150 | 151 | |
|
151 | 152 | colored_record = ''.join([start, def_record, end]) |
|
152 | 153 | return colored_record |
|
153 | 154 | |
|
154 | 155 | |
|
155 | 156 | class ColorFormatterSql(logging.Formatter): |
|
156 | 157 | |
|
157 | 158 | def format(self, record): |
|
158 | 159 | """ |
|
159 | 160 | Changes record's levelname to use with COLORS enum |
|
160 | 161 | """ |
|
161 | 162 | |
|
162 | 163 | start = COLOR_SEQ % (COLORS['SQL']) |
|
163 | 164 | def_record = format_sql(logging.Formatter.format(self, record)) |
|
164 | 165 | end = RESET_SEQ |
|
165 | 166 | |
|
166 | 167 | colored_record = ''.join([start, def_record, end]) |
|
167 | 168 | return colored_record |
|
168 | 169 | |
|
169 | 170 | # marcink: needs to stay with this name for backward .ini compatability |
|
170 | 171 | Pyro4AwareFormatter = ExceptionAwareFormatter |
General Comments 0
You need to be logged in to leave comments.
Login now