Mercurial > kallithea
annotate pylons_app/lib/colored_formatter.py @ 506:d5efb83590ef
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!
added tredning languages stats
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 27 Sep 2010 02:17:03 +0200 |
parents | 779dabcaae28 |
children |
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 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 # Sequences |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 RESET_SEQ = "\033[0m" |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 COLOR_SEQ = "\033[1;%dm" |
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 = { |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 'CRITICAL': MAGENTA, # level 50 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 'ERROR': RED, # level 40 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 'WARNING': CYAN, # level 30 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 'INFO': GREEN, # level 20 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 'DEBUG': BLUE, # level 10 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 } |
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 class ColorFormatter(logging.Formatter): |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 def __init__(self, *args, **kwargs): |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # 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
|
23 logging.Formatter.__init__(self, *args, **kwargs) |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 def format(self, record): |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 """ |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 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
|
28 """ |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 levelname = record.levelname |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 start = COLOR_SEQ % (COLORS[levelname]) |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 def_record = logging.Formatter.format(self, record) |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 end = RESET_SEQ |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 colored_record = start + def_record + end |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 return colored_record |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 logging.ColorFormatter = ColorFormatter |