annotate rhodecode/lib/colored_formatter.py @ 4147:1c8f818787b3 rhodecode-2.2.5-gpl

old style: show the full link box on summary page - no overlap or truncation
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jul 2014 19:03:23 -0400
parents ffd45b185016
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
1 # -*- coding: utf-8 -*-
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
2 # This program is free software: you can redistribute it and/or modify
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
3 # it under the terms of the GNU General Public License as published by
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
4 # the Free Software Foundation, either version 3 of the License, or
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
5 # (at your option) any later version.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
6 #
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
7 # This program is distributed in the hope that it will be useful,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
10 # GNU General Public License for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
11 #
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
12 # You should have received a copy of the GNU General Public License
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 1512
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 import logging
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1186
diff changeset
19 # Sequences
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 RESET_SEQ = "\033[0m"
1391
5f80cc29ca07 Removed bolds from colored logs (better readability)
Marcin Kuzminski <marcin@python-works.com>
parents: 1360
diff changeset
21 COLOR_SEQ = "\033[0;%dm"
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 BOLD_SEQ = "\033[1m"
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 COLORS = {
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
25 'CRITICAL': MAGENTA,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
26 'ERROR': RED,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
27 'WARNING': CYAN,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
28 'INFO': GREEN,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
29 'DEBUG': BLUE,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
30 'SQL': YELLOW
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 }
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
33
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
34 def one_space_trim(s):
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
35 if s.find(" ") == -1:
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
36 return s
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
37 else:
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
38 s = s.replace(' ', ' ')
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
39 return one_space_trim(s)
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
40
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
41
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
42 def format_sql(sql):
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
43 sql = sql.replace('\n', '')
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
44 sql = one_space_trim(sql)
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
45 sql = sql\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
46 .replace(',', ',\n\t')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
47 .replace('SELECT', '\n\tSELECT \n\t')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
48 .replace('UPDATE', '\n\tUPDATE \n\t')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
49 .replace('DELETE', '\n\tDELETE \n\t')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
50 .replace('FROM', '\n\tFROM')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
51 .replace('ORDER BY', '\n\tORDER BY')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
52 .replace('LIMIT', '\n\tLIMIT')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
53 .replace('WHERE', '\n\tWHERE')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
54 .replace('AND', '\n\tAND')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
55 .replace('LEFT', '\n\tLEFT')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
56 .replace('INNER', '\n\tINNER')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
57 .replace('INSERT', '\n\tINSERT')\
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
58 .replace('DELETE', '\n\tDELETE')
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
59 return sql
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
60
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
61
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 class ColorFormatter(logging.Formatter):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 def __init__(self, *args, **kwargs):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 # can't do super(...) here because Formatter is an old school class
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 logging.Formatter.__init__(self, *args, **kwargs)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 def format(self, record):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 """
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 Changes record's levelname to use with COLORS enum
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 """
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
72
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 levelname = record.levelname
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 start = COLOR_SEQ % (COLORS[levelname])
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 def_record = logging.Formatter.format(self, record)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 end = RESET_SEQ
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
77
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
78 colored_record = ''.join([start, def_record, end])
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 return colored_record
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
81
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
82 class ColorFormatterSql(logging.Formatter):
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
83
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
84 def __init__(self, *args, **kwargs):
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
85 # can't do super(...) here because Formatter is an old school class
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
86 logging.Formatter.__init__(self, *args, **kwargs)
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
87
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
88 def format(self, record):
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
89 """
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
90 Changes record's levelname to use with COLORS enum
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
91 """
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
92
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
93 start = COLOR_SEQ % (COLORS['SQL'])
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
94 def_record = format_sql(logging.Formatter.format(self, record))
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
95 end = RESET_SEQ
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
96
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
97 colored_record = ''.join([start, def_record, end])
1186
b9ea10d3e419 timperproxy will just measure the time of queries, and formatting will be handled by the new sql_formatter of queries from sqlalchemy itself. Updated ini files for new way logging
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
98 return colored_record