Mercurial > libervia-backend
annotate src/core/log.py @ 1006:325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 05 May 2014 18:58:34 +0200 |
parents | b4af31a8a4f2 |
children | a7d33c7a8277 |
rev | line source |
---|---|
991 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SàT: a XMPP client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """High level logging functions""" | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed. |
991 | 22 |
23 from sat.core.constants import Const as C | |
24 from sat.core import exceptions | |
25 | |
26 _backend = None | |
27 _loggers = {} | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
28 _handlers = {} |
991 | 29 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
30 class Filtered(Exception): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
31 pass |
991 | 32 |
33 class Logger(object): | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
34 """High level logging class""" |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
35 fmt = None |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
36 filter_name = None |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
37 post_treat = None |
991 | 38 |
39 def __init__(self, name): | |
40 self._name = name | |
41 | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
42 def log(self, level, message): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
43 """Print message |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
44 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
45 @param level: one of C.LOG_LEVELS |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
46 @param message: message to format and print |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
47 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
48 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
49 formatted = self.format(level, message) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
50 if self.post_treat is None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
51 print formatted |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
52 else: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
53 print self.post_treat(level, formatted) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
54 except Filtered: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
55 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
56 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
57 def format(self, level, message): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
58 """Format message according to Logger.fmt |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
59 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
60 @param level: one of C.LOG_LEVELS |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
61 @param message: message to format |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
62 @return: formatted message |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
63 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
64 @raise: Filtered the message must not be logged |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
65 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
66 if self.fmt is None and self.filter_name is None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
67 return message |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
68 record = {'name': self._name, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
69 'message': message, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
70 'levelname': level, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
71 } |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
72 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
73 if not self.filter_name.dictFilter(record): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
74 raise Filtered |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
75 except AttributeError: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
76 if self.filter_name is not None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
77 raise ValueError("Bad filter: filters must have a .filter method") |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
78 return self.fmt % record |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
79 |
991 | 80 def debug(self, msg): |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
81 self.log(C.LOG_LVL_DEBUG, msg) |
991 | 82 |
83 def info(self, msg): | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
84 self.log(C.LOG_LVL_INFO, msg) |
991 | 85 |
86 def warning(self, msg): | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
87 self.log(C.LOG_LVL_WARNING, msg) |
991 | 88 |
89 def error(self, msg): | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
90 self.log(C.LOG_LVL_ERROR, msg) |
991 | 91 |
92 def critical(self, msg): | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
93 self.log(C.LOG_LVL_CRITICAL, msg) |
991 | 94 |
95 | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
96 class FilterName(object): |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
97 """Filter on logger name according to a regex""" |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
98 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
99 def __init__(self, name_re): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
100 """Initialise name filter |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
101 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
102 @param name_re: regular expression used to filter names (using search and not match) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
103 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
104 assert name_re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
105 import re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
106 self.name_re = re.compile(name_re) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
107 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
108 def filter(self, record): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
109 if self.name_re.search(record.name) is not None: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
110 return 1 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
111 return 0 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
112 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
113 def dictFilter(self, dict_record): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
114 """Filter using a dictionary record |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
115 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
116 @param dict_record: dictionary with at list a key "name" with logger name |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
117 @return: True if message should be logged |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
118 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
119 class LogRecord(object): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
120 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
121 log_record = LogRecord() |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
122 log_record.name = dict_record['name'] |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
123 return self.filter(log_record) == 1 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
124 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
125 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
126 def _ansiColors(level, message): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
127 """Colorise message depending on level for terminals |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
128 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
129 @param level: one of C.LOG_LEVELS |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
130 @param message: formatted message to log |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
131 @return: message with ANSI escape codes for coloration |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
132 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
133 if level == C.LOG_LVL_DEBUG: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
134 out = (C.ANSI_FG_CYAN, message, C.ANSI_RESET) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
135 elif level == C.LOG_LVL_WARNING: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
136 out = (C.ANSI_FG_YELLOW, message, C.ANSI_RESET) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
137 elif level == C.LOG_LVL_ERROR: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
138 out = (C.ANSI_FG_RED, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
139 C.ANSI_BLINK, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
140 r'/!\ ', |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
141 C.ANSI_BLINK_OFF, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
142 message, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
143 C.ANSI_RESET) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
144 elif level == C.LOG_LVL_CRITICAL: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
145 out = (C.ANSI_BOLD, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
146 C.ANSI_FG_RED, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
147 'Guru Meditation ', |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
148 C.ANSI_NORMAL_WEIGHT, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
149 message, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
150 C.ANSI_RESET) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
151 else: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
152 out = message |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
153 return ''.join(out) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
154 |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
155 def _manageOutputs(outputs_raw): |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
156 """ Parse output option in a backend agnostic way, and fill _handlers consequently |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
157 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
158 @param outputs_raw: output option as enterred in environment variable or in configuration |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
159 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
160 if not outputs_raw: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
161 return |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
162 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
163 global _handlers |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
164 if len(outputs) == 1: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
165 _handlers[C.LOG_OPT_OUTPUT_FILE] = outputs.pop() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
166 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
167 for output in outputs: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
168 if not output: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
169 continue |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
170 if output[-1] == ')': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
171 # we have options |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
172 opt_begin = output.rfind('(') |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
173 options = output[opt_begin+1:-1] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
174 output = output[:opt_begin] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
175 else: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
176 options = None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
177 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
178 if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
179 raise ValueError(u"Invalid output [%s]" % output) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
180 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
181 if output == C.LOG_OPT_OUTPUT_DEFAULT: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
182 # no option for defaut handler |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
183 _handlers[output] = None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
184 elif output == C.LOG_OPT_OUTPUT_FILE: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
185 if not options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
186 ValueError("%(handler)s output need a path as option" % {'handler': output}) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
187 _handlers[output] = options |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
188 options = None # option are parsed, we can empty them |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
189 elif output == C.LOG_OPT_OUTPUT_MEMORY: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
190 # we have memory handler, option can be the len limit or None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
191 try: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
192 limit = int(options) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
193 options = None # option are parsed, we can empty them |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
194 except (TypeError, ValueError): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
195 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
196 _handlers[output] = limit |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
197 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
198 if options: # we should not have unparsed options |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
199 raise ValueError(u"options [%(options)s] are not supported for %(handler)s output" % {'options': options, 'handler': output}) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
200 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
201 def _configureStdLogging(logging, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False): |
991 | 202 """Configure standard logging module |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
203 |
991 | 204 @param logging: standard logging module |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
205 @param level: one of C.LOG_LEVELS |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
206 @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
207 - "message" |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
208 - "levelname" |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
209 - "name" (logger name) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
210 @param logger: if set, use it as a regular expression to filter on logger name. |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
211 Use search to match expression, so ^ or $ can be necessary. |
991 | 212 @param colors: if True use ANSI colors to show log levels |
213 @param force_colors: if True ANSI colors are used even if stdout is not a tty | |
214 """ | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
215 format_ = fmt |
991 | 216 if level is None: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
217 level = C.LOG_LVL_DEBUG |
991 | 218 import sys |
219 with_color = colors & (sys.stdout.isatty() or force_colors) | |
220 if not colors and force_colors: | |
221 raise ValueError("force_colors can't be used if colors is False") | |
222 | |
223 class SatFormatter(logging.Formatter): | |
224 u"""Formatter which manage SàT specificities""" | |
225 | |
226 def __init__(self, fmt=None, datefmt=None): | |
227 super(SatFormatter, self).__init__(fmt, datefmt) | |
228 | |
229 def format(self, record): | |
230 s = super(SatFormatter, self).format(record) | |
231 if with_color: | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
232 s = _ansiColors(record.levelname, s) |
991 | 233 return s |
234 | |
235 root_logger = logging.getLogger() | |
236 if len(root_logger.handlers) == 0: | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
237 _manageOutputs(output) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
238 formatter = SatFormatter(format_) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
239 name_filter = FilterName(logger) if logger else None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
240 for handler, options in _handlers.items(): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
241 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
242 hdlr = logging.StreamHandler() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
243 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
244 import logging.handlers |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
245 hdlr = logging.handlers.BufferingHandler(options) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
246 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
247 elif handler == C.LOG_OPT_OUTPUT_FILE: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
248 import os.path |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
249 hdlr = logging.FileHandler(os.path.expanduser(options)) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
250 else: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
251 raise ValueError("Unknown handler type") |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
252 hdlr.setFormatter(formatter) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
253 root_logger.addHandler(hdlr) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
254 root_logger.setLevel(level) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
255 if name_filter is not None: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
256 hdlr.addFilter(name_filter) |
991 | 257 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
258 root_logger.warning(u"Handlers already set on root logger") |
991 | 259 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
260 def _configureBasic(level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
261 """Configure basic backend |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
262 @param level: same as _configureStdLogging.level |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
263 @param fmt: same as _configureStdLogging.fmt |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
264 @param output: not implemented yet TODO |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
265 @param logger: same as _configureStdLogging.logger |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
266 @param colors: same as _configureStdLogging.colors |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
267 @param force_colors: same as _configureStdLogging.force_colors |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
268 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
269 if level is not None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
270 # we deactivate methods below level |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
271 level_idx = C.LOG_LEVELS.index(level) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
272 def dev_null(self, msg): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
273 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
274 for _level in C.LOG_LEVELS[:level_idx]: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
275 setattr(Logger, _level.lower(), dev_null) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
276 if fmt is not None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
277 if fmt != '%(message)s': # %(message)s is the same as None |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
278 Logger.fmt = fmt |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
279 if output is not None: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
280 if output != C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
281 # TODO: manage other outputs |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
282 raise NotImplementedError("Basic backend only manage default output yet") |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
283 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
284 if logger: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
285 Logger.filter_name = FilterName(logger) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
286 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
287 if colors: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
288 import sys |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
289 if force_colors or sys.stdout.isatty(): |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
290 # we need colors |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
291 Logger.post_treat = lambda self, level, message: _ansiColors(level, message) |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
292 elif force_colors: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
293 raise ValueError("force_colors can't be used if colors is False") |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
294 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
295 def configure(backend=C.LOG_BACKEND_STANDARD, **options): |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
296 """Configure logging behaviour |
991 | 297 @param backend: can be: |
298 C.LOG_BACKEND_STANDARD: use standard logging module | |
299 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
300 C.LOG_BACKEND_BASIC: use a basic print based logging | |
301 """ | |
302 global _backend | |
303 if _backend is not None: | |
304 raise exceptions.InternalError("Logging can only be configured once") | |
305 _backend = backend | |
306 | |
992
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
307 if backend in (C.LOG_BACKEND_TWISTED, C.LOG_BACKEND_STANDARD): |
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
308 if backend == C.LOG_BACKEND_TWISTED: |
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
309 from twisted.python import log |
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
310 observer = log.PythonLoggingObserver() |
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
311 observer.start() |
991 | 312 global getLogger |
313 global debug | |
314 global info | |
315 global warning | |
316 global error | |
317 global critical | |
318 import logging | |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
319 _configureStdLogging(logging, **options) |
991 | 320 getLogger = logging.getLogger |
321 debug = logging.debug | |
322 info = logging.info | |
323 warning = logging.warning | |
324 error = logging.error | |
325 critical = logging.critical | |
326 | |
327 elif backend == C.LOG_BACKEND_BASIC: | |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
328 _configureBasic(**options) |
991 | 329 |
330 else: | |
331 raise ValueError("unknown backend") | |
332 | |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
333 def _parseOptions(options): |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
334 """Parse string options as given in conf or environment variable, and return expected python value |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
335 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
336 @param options (dict): options with (key: name, value: string value) |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
337 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
338 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
339 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
340 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
341 if COLORS in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
342 if options[COLORS].lower() in ('1', 'true'): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
343 options[COLORS] = True |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
344 elif options[COLORS] == 'force': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
345 options[COLORS] = True |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
346 options['force_colors'] = True |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
347 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
348 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
349 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
350 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
351 if level not in C.LOG_LEVELS: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
352 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
353 options[LEVEL] = level |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
354 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
355 def satConfigure(backend=C.LOG_BACKEND_TWISTED): |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
356 """Configure logging system for SàT, can be used by frontends |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
357 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
358 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
359 """ |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
360 import ConfigParser |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
361 import os |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
362 log_conf = {} |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
363 config = ConfigParser.SafeConfigParser() |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
364 config.read(C.CONFIG_FILES) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
365 for opt_name, opt_default in C.LOG_OPTIONS: |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
366 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
367 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))] |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
368 except KeyError: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
369 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
370 log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name) |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
371 except ConfigParser.NoOptionError: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
372 log_conf[opt_name] = opt_default |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
373 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
374 _parseOptions(log_conf) |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
375 configure(backend, **log_conf) |
991 | 376 |
377 def getLogger(name=C.LOG_BASE_LOGGER): | |
378 return _loggers.setdefault(name, Logger(name)) | |
379 | |
380 _root_logger = getLogger() | |
381 | |
382 def debug(msg): | |
383 _root_logger.debug(msg) | |
384 | |
385 def info(msg): | |
386 _root_logger.info(msg) | |
387 | |
388 def warning(msg): | |
389 _root_logger.warning(msg) | |
390 | |
391 def error(msg): | |
392 _root_logger.error(msg) | |
393 | |
394 def critical(msg): | |
395 _root_logger.critical(msg) |