annotate rhodecode/lib/colored_formatter.py @ 3774:60335b702a00 beta

invalidation: don't create CacheInvalidation records on startup Creating the records early gave an advantage before lightweight was introduced. With lightweight it is no longer necessary. The records will be created on demand anyway and there is no reason to create and maintain them before they are used.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 03 Apr 2013 15:56:12 +0200
parents 5f80cc29ca07
children bf263968da47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 import logging
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 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
5
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1186
diff changeset
6 # Sequences
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 RESET_SEQ = "\033[0m"
1391
5f80cc29ca07 Removed bolds from colored logs (better readability)
Marcin Kuzminski <marcin@python-works.com>
parents: 1360
diff changeset
8 COLOR_SEQ = "\033[0;%dm"
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 BOLD_SEQ = "\033[1m"
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 COLORS = {
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
12 'CRITICAL': MAGENTA,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
13 'ERROR': RED,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
14 'WARNING': CYAN,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
15 'INFO': GREEN,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
16 'DEBUG': BLUE,
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
17 'SQL': YELLOW
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 }
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
20
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
21 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
22 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
23 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
24 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
25 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
26 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
27
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
28
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
29 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
30 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
31 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
32 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
33 .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
34 .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
35 .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
36 .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
37 .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
38 .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
39 .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
40 .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
41 .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
42 .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
43 .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
44 .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
45 .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
46 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
47
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
48
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 class ColorFormatter(logging.Formatter):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 def __init__(self, *args, **kwargs):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 # 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
53 logging.Formatter.__init__(self, *args, **kwargs)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 def format(self, record):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 """
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 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
58 """
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
59
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 levelname = record.levelname
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 start = COLOR_SEQ % (COLORS[levelname])
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 def_record = logging.Formatter.format(self, record)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 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
64
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
65 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
66 return colored_record
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
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
68
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
69 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
70
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
71 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
72 # 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
73 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
74
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
75 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
76 """
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 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
78 """
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
79
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
80 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
81 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
82 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
83
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
84 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
85 return colored_record