Mercurial > libervia-backend
annotate src/core/log.py @ 1005:b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
- formatting is inspired from, and use when possible, standard logging. "message", "levelname", and "name" are the only format managed, depending on backend more can be managed (standard backend formats are specified in official python logging doc)
- name filter use regular expressions. It's possible to log only plugins with SAT_LOG_LOGGER="^sat.plugins". To log only XEPs 96 & 65, we can use SAT_LOG_LOGGER='(xep_0095|xep_0065)'
- output management use a particular syntax:
- output handler are name with "//", so far there are "//default" (most of time stderr), "//memory" and "//file"
- options can be specified in parenthesis, e.g. "//memory(50)" mean a 50 lines memory buffer (50 is the current default, so that's equivalent to "//memory")
- several handlers can be specified: "//file(/tmp/sat.log)//default" will use the default logging + a the /tmp/sat.log file
- if there is only one handler, it use the file handler: "/tmp/sat.log" is the same as "//file(/tmp/sat.log)"
- not finished, need more work for twisted and basic backends
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 05 May 2014 18:58:34 +0200 |
parents | 652c01ca69b1 |
children | 325fd230c15d |
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 |
30 | |
31 class Logger(object): | |
32 """ High level logging class """ | |
33 | |
34 def __init__(self, name): | |
35 self._name = name | |
36 | |
37 def debug(self, msg): | |
38 print msg | |
39 | |
40 def info(self, msg): | |
41 print msg | |
42 | |
43 def warning(self, msg): | |
44 print msg | |
45 | |
46 def error(self, msg): | |
47 print msg | |
48 | |
49 def critical(self, msg): | |
50 print msg | |
51 | |
52 | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
53 class FilterName(object): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
54 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
55 def __init__(self, name_re): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
56 """Initialise name filter |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
57 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
58 @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
|
59 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
60 assert name_re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
61 import re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
62 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
|
63 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
64 def filter(self, record): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
65 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
|
66 return 1 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
67 return 0 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
68 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
69 def _manageOutputs(outputs_raw): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
70 """ Parse output option in a backend agnostic way, and fill _backend consequently |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
71 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
72 @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
|
73 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
74 if not outputs_raw: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
75 return |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
76 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
|
77 global _handlers |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
78 if len(outputs) == 1: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
79 _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
|
80 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
81 for output in outputs: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
82 if not output: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
83 continue |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
84 if output[-1] == ')': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
85 # we have options |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
86 opt_begin = output.rfind('(') |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
87 options = output[opt_begin+1:-1] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
88 output = output[:opt_begin] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
89 else: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
90 options = None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
91 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
92 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
|
93 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
|
94 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
95 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
|
96 # no option for defaut handler |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
97 _handlers[output] = None |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
98 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
|
99 if not options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
100 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
|
101 _handlers[output] = options |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
102 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
|
103 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
|
104 # 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
|
105 try: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
106 limit = int(options) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
107 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
|
108 except (TypeError, ValueError): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
109 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
|
110 _handlers[output] = limit |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
111 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
112 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
|
113 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
|
114 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
115 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 | 116 """Configure standard logging module |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
117 |
991 | 118 @param logging: standard logging module |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
119 @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
|
120 @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
|
121 - "message" |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
122 - "levelname" |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
123 - "name" (logger name) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
124 @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
|
125 Use search to match expression, so ^ or $ can be necessary. |
991 | 126 @param colors: if True use ANSI colors to show log levels |
127 @param force_colors: if True ANSI colors are used even if stdout is not a tty | |
128 """ | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
129 format_ = fmt |
991 | 130 if level is None: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
131 level = C.LOG_LVL_DEBUG |
991 | 132 import sys |
133 with_color = colors & (sys.stdout.isatty() or force_colors) | |
134 if not colors and force_colors: | |
135 raise ValueError("force_colors can't be used if colors is False") | |
136 | |
137 class SatFormatter(logging.Formatter): | |
138 u"""Formatter which manage SàT specificities""" | |
139 | |
140 def __init__(self, fmt=None, datefmt=None): | |
141 super(SatFormatter, self).__init__(fmt, datefmt) | |
142 | |
143 def format(self, record): | |
144 s = super(SatFormatter, self).format(record) | |
145 if with_color: | |
146 if record.levelno == logging.DEBUG: | |
147 fmt = (C.ANSI_FG_CYAN, s, C.ANSI_RESET) | |
148 elif record.levelno == logging.WARNING: | |
149 fmt = (C.ANSI_FG_YELLOW, s, C.ANSI_RESET) | |
150 elif record.levelno == logging.ERROR: | |
151 fmt = (C.ANSI_FG_RED, | |
152 C.ANSI_BLINK, | |
153 r'/!\ ', | |
154 C.ANSI_BLINK_OFF, | |
155 s, | |
156 C.ANSI_RESET) | |
157 elif record.levelno == logging.CRITICAL: | |
158 fmt = (C.ANSI_BOLD, | |
159 C.ANSI_FG_RED, | |
160 'Guru Meditation ', | |
161 C.ANSI_NORMAL_WEIGHT, | |
162 s, | |
163 C.ANSI_RESET) | |
164 else: | |
165 fmt = s | |
166 s = ''.join(fmt) | |
167 | |
168 return s | |
169 | |
170 root_logger = logging.getLogger() | |
171 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
|
172 _manageOutputs(output) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
173 formatter = SatFormatter(format_) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
174 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
|
175 for handler, options in _handlers.items(): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
176 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
|
177 hdlr = logging.StreamHandler() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
178 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
|
179 import logging.handlers |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
180 hdlr = logging.handlers.BufferingHandler(options) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
181 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
|
182 import os.path |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
183 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
|
184 else: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
185 raise ValueError("Unknown handler type") |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
186 hdlr.setFormatter(formatter) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
187 root_logger.addHandler(hdlr) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
188 root_logger.setLevel(level) |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
189 if name_filter is not None: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
190 hdlr.addFilter(name_filter) |
991 | 191 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
192 root_logger.warning(u"Handlers already set on root logger") |
991 | 193 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
194 def configure(backend=C.LOG_BACKEND_STANDARD, **options): |
991 | 195 """Configure logging bejaviour |
196 @param backend: can be: | |
197 C.LOG_BACKEND_STANDARD: use standard logging module | |
198 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
199 C.LOG_BACKEND_BASIC: use a basic print based logging | |
200 """ | |
201 global _backend | |
202 if _backend is not None: | |
203 raise exceptions.InternalError("Logging can only be configured once") | |
204 _backend = backend | |
205 | |
992
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 observer = log.PythonLoggingObserver() |
f51a1895275c
core (log): twisted backend use logging methods instead of log.msg
Goffi <goffi@goffi.org>
parents:
991
diff
changeset
|
210 observer.start() |
991 | 211 global getLogger |
212 global debug | |
213 global info | |
214 global warning | |
215 global error | |
216 global critical | |
217 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
|
218 _configureStdLogging(logging, **options) |
991 | 219 getLogger = logging.getLogger |
220 debug = logging.debug | |
221 info = logging.info | |
222 warning = logging.warning | |
223 error = logging.error | |
224 critical = logging.critical | |
225 | |
226 elif backend == C.LOG_BACKEND_BASIC: | |
227 pass | |
228 | |
229 else: | |
230 raise ValueError("unknown backend") | |
231 | |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
232 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
|
233 """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
|
234 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
235 @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
|
236 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
237 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
238 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
239 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
240 if COLORS in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
241 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
|
242 options[COLORS] = True |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
243 elif options[COLORS] == 'force': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
244 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
|
245 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
|
246 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
247 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
248 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
249 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
250 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
|
251 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
252 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
|
253 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
254 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
|
255 """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
|
256 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
257 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
|
258 """ |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 config.read(C.CONFIG_FILES) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
264 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
|
265 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
266 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
|
267 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
|
268 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
269 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
|
270 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
|
271 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
|
272 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
273 _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
|
274 configure(backend, **log_conf) |
991 | 275 |
276 def getLogger(name=C.LOG_BASE_LOGGER): | |
277 return _loggers.setdefault(name, Logger(name)) | |
278 | |
279 _root_logger = getLogger() | |
280 | |
281 def debug(msg): | |
282 _root_logger.debug(msg) | |
283 | |
284 def info(msg): | |
285 _root_logger.info(msg) | |
286 | |
287 def warning(msg): | |
288 _root_logger.warning(msg) | |
289 | |
290 def error(msg): | |
291 _root_logger.error(msg) | |
292 | |
293 def critical(msg): | |
294 _root_logger.critical(msg) |