Mercurial > libervia-backend
annotate src/core/log.py @ 1010:73a0b7f94674
primitivus: use of new logging system:
- default output is \\memory
- logs can be seen with :messages command
- now useless writeLog method has been removed
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 05 May 2014 20:12:19 +0200 |
parents | d70d4fe5c5f8 |
children | c8771279497e |
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 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
30 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
31 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
|
32 pass |
991 | 33 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
34 |
991 | 35 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
|
36 """High level logging class""" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
37 fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
38 filter_name = None # filter to call |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
39 post_treat = None |
991 | 40 |
41 def __init__(self, name): | |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
42 if isinstance(name, Logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
43 self.copy(name) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
44 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
45 self._name = name |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
46 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
47 def copy(self, other): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
48 """Copy values from other Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
49 self.fmt = other.fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
50 self.Filter_name = other.fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
51 self.post_treat = other.post_treat |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
52 self._name = other._name |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
53 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
54 def out(self, message, level=None): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
55 """Actually log the message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
56 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
57 @param message: formatted message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
58 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
59 print message |
991 | 60 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
61 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
|
62 """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
|
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 @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
|
65 @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
|
66 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
67 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
68 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
|
69 if self.post_treat is None: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
70 self.out(formatted, level) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
71 else: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
72 self.out(self.post_treat(level, formatted), level) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
73 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
|
74 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
75 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
76 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
|
77 """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
|
78 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
79 @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
|
80 @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
|
81 @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
|
82 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
83 @raise: Filtered when the message must not be logged |
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 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 '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
|
89 '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
|
90 } |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
91 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 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
|
96 raise ValueError("Bad filter: filters must have a .filter method") |
1008
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
97 try: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
98 return self.fmt % record |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
99 except TypeError: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
100 return message |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
101 except KeyError as e: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
102 if e.args[0] == 'profile': |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
103 # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
104 record['profile'] = _getProfile() |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
105 return self.fmt % record |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
106 else: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
107 raise e |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
108 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
109 |
991 | 110 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
|
111 self.log(C.LOG_LVL_DEBUG, msg) |
991 | 112 |
113 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
|
114 self.log(C.LOG_LVL_INFO, msg) |
991 | 115 |
116 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
|
117 self.log(C.LOG_LVL_WARNING, msg) |
991 | 118 |
119 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
|
120 self.log(C.LOG_LVL_ERROR, msg) |
991 | 121 |
122 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
|
123 self.log(C.LOG_LVL_CRITICAL, msg) |
991 | 124 |
125 | |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
126 class TwistedLogger(Logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
127 colors = True |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
128 force_colors = False |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
129 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
130 def __init__(self, *args, **kwargs): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
131 super(TwistedLogger, self).__init__(*args, **kwargs) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
132 from twisted.python import log |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
133 self.twisted_log = log |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
134 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
135 def out(self, message, level=None): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
136 """Actually log the message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
137 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
138 @param message: formatted message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
139 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
140 self.twisted_log.msg(message.encode('utf-8', 'ignore'), sat_logged=True, level=level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
141 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
142 |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
143 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
|
144 """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
|
145 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
146 def __init__(self, name_re): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
147 """Initialise name filter |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
148 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
149 @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
|
150 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
151 assert name_re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
152 import re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
153 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
|
154 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
155 def filter(self, record): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
156 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
|
157 return 1 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
158 return 0 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
159 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
160 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
|
161 """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
|
162 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
163 @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
|
164 @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
|
165 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
166 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
|
167 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
172 def memoryGet(size=None): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
173 """Return buffered logs |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
174 |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
175 @param size: number of logs to return |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
176 """ |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
177 if not C.LOG_OPT_OUTPUT_MEMORY in _handlers: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
178 raise ValueError('memory output is not used') |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
179 if _backend == C.LOG_BACKEND_STANDARD: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
180 mem_handler = _handlers[C.LOG_OPT_OUTPUT_MEMORY] |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
181 return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:]) |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
182 else: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
183 raise NotImplementedError |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
184 |
1008
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
185 def _getProfile(): |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
186 """Try to find profile value using introspection""" |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
187 import inspect |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
188 stack = inspect.stack() |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
189 current_path = stack[0][1] |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
190 for frame_data in stack[:-1]: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
191 if frame_data[1] != current_path: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
192 break |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
193 |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
194 frame = frame_data[0] |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
195 args = inspect.getargvalues(frame) |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
196 try: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
197 profile = args.locals.get('profile') or args.locals['profile_key'] |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
198 except (TypeError, KeyError): |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
199 try: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
200 try: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
201 profile = args.locals['self'].profile |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
202 except AttributeError: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
203 profile = args.locals['self'].parent.profile |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
204 except Exception: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
205 # we can't find profile, we return an empty value |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
206 profile = '' |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
207 return profile |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
208 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
209 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
|
210 """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
|
211 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
212 @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
|
213 @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
|
214 @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
|
215 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 r'/!\ ', |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
224 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
|
225 message, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 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
|
230 '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
|
231 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
|
232 message, |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
233 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
|
234 else: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
235 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
|
236 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
|
237 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
238 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
239 class Configure(object): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
240 LOGGER_CLASS = None |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
241 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
242 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
243 """Configure backend |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
244 @param level: one of C.LOG_LEVELS |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
245 @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
246 - "message" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
247 - "levelname" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
248 - "name" (logger name) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
249 @param logger: if set, use it as a regular expression to filter on logger name. |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
250 Use search to match expression, so ^ or $ can be necessary. |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
251 @param colors: if True use ANSI colors to show log levels |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
252 @param force_colors: if True ANSI colors are used even if stdout is not a tty |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
253 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
254 self.preTreatment() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
255 self.configureLevel(level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
256 self.configureFormat(fmt) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
257 self.configureOutput(output) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
258 self.configureLogger(logger) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
259 self.configureColors(colors, force_colors) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
260 self.postTreatment() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
261 self.updateCurrentLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
262 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
263 def updateCurrentLogger(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
264 """update existing logger to the class needed for this backend""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
265 if self.LOGGER_CLASS is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
266 return |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
267 for name, logger in _loggers.items(): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
268 _loggers[name] = self.LOGGER_CLASS(logger) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
269 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
270 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
271 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
272 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
273 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
274 pass |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
275 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
276 def configureFormat(self, fmt): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
277 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
278 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
279 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
280 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
281 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
282 def configureLogger(self, logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
283 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
284 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
285 def configureColors(self, colors, force_colors): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
286 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
287 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
288 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
289 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
290 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
291 def manageOutputs(self, outputs_raw): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
292 """ 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
|
293 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
294 @param outputs_raw: output option as enterred in environment variable or in configuration |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
295 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
296 if not outputs_raw: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
297 return |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
298 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
299 global _handlers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
300 if len(outputs) == 1: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
301 _handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()] |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
302 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
303 for output in outputs: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
304 if not output: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
305 continue |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
306 if output[-1] == ')': |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
307 # we have options |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
308 opt_begin = output.rfind('(') |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
309 options = output[opt_begin+1:-1] |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
310 output = output[:opt_begin] |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
311 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
312 options = None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
313 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
314 if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
315 raise ValueError(u"Invalid output [%s]" % output) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
316 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
317 if output == C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
318 # no option for defaut handler |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
319 _handlers[output] = None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
320 elif output == C.LOG_OPT_OUTPUT_FILE: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
321 if not options: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
322 ValueError("%(handler)s output need a path as option" % {'handler': output}) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
323 _handlers.setdefault(output, []).append(options) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
324 options = None # option are parsed, we can empty them |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
325 elif output == C.LOG_OPT_OUTPUT_MEMORY: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
326 # we have memory handler, option can be the len limit or None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
327 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
328 limit = int(options) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
329 options = None # option are parsed, we can empty them |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
330 except (TypeError, ValueError): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
331 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
332 _handlers[output] = limit |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
333 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
334 if options: # we should not have unparsed options |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
335 raise ValueError(u"options [%(options)s] are not supported for %(handler)s output" % {'options': options, 'handler': output}) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
336 |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
337 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
338 class ConfigureBasic(Configure): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
339 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
340 if level is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
341 # we deactivate methods below level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
342 level_idx = C.LOG_LEVELS.index(level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
343 def dev_null(self, msg): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
344 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
345 for _level in C.LOG_LEVELS[:level_idx]: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
346 setattr(Logger, _level.lower(), dev_null) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
347 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
348 def configureFormat(self, fmt): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
349 if fmt is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
350 if fmt != '%(message)s': # %(message)s is the same as None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
351 Logger.fmt = fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
352 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
353 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
354 if output is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
355 if output != C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
356 # TODO: manage other outputs |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
357 raise NotImplementedError("Basic backend only manage default output yet") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
358 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
359 def configureLogger(self, logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
360 if logger: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
361 Logger.filter_name = FilterName(logger) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
362 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
363 def configureColors(self, colors, force_colors): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
364 if colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
365 import sys |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
366 if force_colors or sys.stdout.isatty(): # FIXME: isatty should be tested on each handler, not globaly |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
367 # we need colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
368 Logger.post_treat = lambda self, level, message: _ansiColors(level, message) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
369 elif force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
370 raise ValueError("force_colors can't be used if colors is False") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
371 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
372 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
373 class ConfigureTwisted(ConfigureBasic): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
374 LOGGER_CLASS = TwistedLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
375 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
376 def changeObserver(self, observer, can_color=False): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
377 """Install a hook on observer to manage SàT specificities |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
378 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
379 @param observer: original observer to hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
380 @param can_color: True if observer can display ansi colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
381 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
382 def observer_hook(event): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
383 """redirect non SàT log to twisted_logger, and add colors when possible""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
384 if 'sat_logged' in event: # we only want our own logs, other are managed by twistedObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
385 # we add colors if possible |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
386 if (can_color and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
387 message = event.get('message', tuple()) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
388 level = event.get('level', C.LOG_LVL_INFO) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
389 if message: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
390 event['message'] = (_ansiColors(level, ''.join(message)),) # must be a tuple |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
391 observer(event) # we can now call the original observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
392 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
393 return observer_hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
394 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
395 def changeFileLogObserver(self, observer): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
396 """Install SàT hook for FileLogObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
397 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
398 if the output is a tty, we allow colors, else we don't |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
399 @param observer: original observer to hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
400 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
401 log_obs = observer.__self__ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
402 log_file = log_obs.write.__self__ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
403 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
404 can_color = log_file.isatty() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
405 except AttributeError: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
406 can_color = False |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
407 return self.changeObserver(observer, can_color=can_color) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
408 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
409 def installObserverHook(self, observer): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
410 """Check observer type and install SàT hook when possible |
991 | 411 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
412 @param observer: observer to hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
413 @return: hooked observer or original one |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
414 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
415 if hasattr(observer, '__self__'): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
416 ori = observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
417 if isinstance(observer.__self__, self.log.FileLogObserver): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
418 observer = self.changeFileLogObserver(observer) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
419 elif isinstance(observer.__self__, self.log.DefaultObserver): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
420 observer = self.changeObserver(observer, can_color=True) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
421 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
422 # we use print because log system is not fully initialized |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
423 print("Unmanaged observer [%s]" % observer) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
424 return observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
425 self.observers[ori] = observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
426 return observer |
991 | 427 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
428 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
429 """initialise needed attributes, and install observers hooks""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
430 self.observers = {} |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
431 from twisted.python import log |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
432 self.log = log |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
433 self.log_publisher = log.msg.__self__ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
434 def addObserverObserver(self_logpub, other): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
435 """Install hook so we know when a new observer is added""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
436 other = self.installObserverHook(other) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
437 return self_logpub._originalAddObserver(other) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
438 def removeObserverObserver(self_logpub, ori): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
439 """removeObserver hook fix |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
440 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
441 As we wrap the original observer, the original removeObserver may want to remove the original object instead of the wrapper, this method fix this |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
442 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
443 if ori in self.observers: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
444 self_logpub._originalRemoveObserver(self.observers[ori]) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
445 else: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
446 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
447 self_logpub._originalRemoveObserver(ori) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
448 except ValueError: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
449 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
450 ori in self.cleared_observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
451 except AttributeError: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
452 raise ValueError("Unknown observer") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
453 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
454 # we replace addObserver/removeObserver by our own |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
455 log.LogPublisher._originalAddObserver = log.LogPublisher.addObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
456 log.LogPublisher._originalRemoveObserver = log.LogPublisher.removeObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
457 import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
458 log.addObserver = types.MethodType(addObserverObserver, self.log_publisher, log.LogPublisher) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
459 log.removeObserver = types.MethodType(removeObserverObserver, self.log_publisher, log.LogPublisher) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
460 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
461 # we now change existing observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
462 for idx, observer in enumerate(self.log_publisher.observers): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
463 self.log_publisher.observers[idx] = self.installObserverHook(observer) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
464 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
465 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
466 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
467 self.LOGGER_CLASS.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
468 super(ConfigureTwisted, self).configureLevel(level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
469 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
470 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
471 import sys |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
472 if output is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
473 output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
474 self.manageOutputs(output) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
475 addObserver = self.log.addObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
476 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
477 if C.LOG_OPT_OUTPUT_DEFAULT in _handlers: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
478 # default output is already managed, we just add output to stdout if we are in debug mode |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
479 from twisted.internet import defer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
480 if defer.Deferred.debug: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
481 addObserver(self.log.FileLogObserver(sys.stdout).emit) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
482 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
483 # \\default is not in the output, so we remove current observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
484 self.cleared_observers = self.log_publisher.observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
485 self.observers.clear() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
486 del self.log_publisher.observers[:] |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
487 # and we forbid twistd to add any observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
488 self.log.addObserver = lambda other: None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
489 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
490 if C.LOG_OPT_OUTPUT_FILE in _handlers: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
491 from twisted.python import logfile |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
492 for path in _handlers[C.LOG_OPT_OUTPUT_FILE]: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
493 log_file = sys.stdout if path == '-' else logfile.LogFile.fromFullPath(path) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
494 addObserver(self.log.FileLogObserver(log_file).emit) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
495 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
496 if C.LOG_OPT_OUTPUT_MEMORY in _handlers: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
497 raise NotImplementedError("Memory observer is not implemented in Twisted backend") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
498 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
499 def configureColors(self, colors, force_colors): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
500 self.LOGGER_CLASS.colors = colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
501 self.LOGGER_CLASS.force_colors = force_colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
502 if force_colors and not colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
503 raise ValueError('colors must be True if force_colors is True') |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
504 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
505 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
506 """Install twistedObserver which manage non SàT logs""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
507 def twistedObserver(event): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
508 """Observer which redirect log message not produced by SàT to SàT logging system""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
509 if not 'sat_logged' in event: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
510 # this log was not produced by SàT |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
511 from twisted.python import log |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
512 text = log.textFromEventDict(event) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
513 if text is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
514 return |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
515 twisted_logger = getLogger(C.LOG_TWISTED_LOGGER) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
516 log_method = twisted_logger.error if event.get('isError', False) else twisted_logger.info |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
517 log_method(text.decode('utf-8')) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
518 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
519 self.log_publisher._originalAddObserver(twistedObserver) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
520 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
521 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
522 class ConfigureStandard(Configure): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
523 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
524 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
525 if fmt is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
526 fmt = C.LOG_OPT_FORMAT[1] |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
527 if output is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
528 output = C.LOG_OPT_OUTPUT[1] |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
529 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors) |
991 | 530 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
531 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
532 """We use logging methods directly, instead of using Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
533 global getLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
534 global debug |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
535 global info |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
536 global warning |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
537 global error |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
538 global critical |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
539 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
540 getLogger = logging.getLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
541 debug = logging.debug |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
542 info = logging.info |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
543 warning = logging.warning |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
544 error = logging.error |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
545 critical = logging.critical |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
546 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
547 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
548 if level is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
549 level = C.LOG_LVL_DEBUG |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
550 self.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
551 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
552 def configureFormat(self, fmt): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
553 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
554 format_ = fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
555 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
556 class SatFormatter(logging.Formatter): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
557 u"""Formatter which manage SàT specificities""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
558 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
559 def __init__(self, fmt=None, datefmt=None): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
560 super(SatFormatter, self).__init__(fmt, datefmt) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
561 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
562 def format(self, record): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
563 s = super(SatFormatter, self).format(record) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
564 if self.with_color: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
565 s = _ansiColors(record.levelname, s) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
566 return s |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
567 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
568 self.formatter = SatFormatter(format_) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
569 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
570 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
571 self.manageOutputs(output) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
572 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
573 def configureLogger(self, logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
574 self.name_filter = FilterName(logger) if logger else None |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
575 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
576 def configureColors(self, colors, force_colors): |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
577 import sys |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
578 self.formatter.with_color = colors & (sys.stdout.isatty() or force_colors) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
579 if not colors and force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
580 raise ValueError("force_colors can't be used if colors is False") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
581 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
582 def _addHandler(self, root_logger, hdlr): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
583 hdlr.setFormatter(self.formatter) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
584 root_logger.addHandler(hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
585 root_logger.setLevel(self.level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
586 if self.name_filter is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
587 hdlr.addFilter(self.name_filter) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
588 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
589 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
590 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
591 root_logger = logging.getLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
592 if len(root_logger.handlers) == 0: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
593 for handler, options in _handlers.items(): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
594 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
595 hdlr = logging.StreamHandler() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
596 self._addHandler(root_logger, hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
597 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
598 from logging.handlers import BufferingHandler |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
599 class SatMemoryHandler(BufferingHandler): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
600 def emit(self, record): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
601 super(SatMemoryHandler, self).emit(self.format(record)) |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
602 hdlr = SatMemoryHandler(options) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
603 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
604 self._addHandler(root_logger, hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
605 elif handler == C.LOG_OPT_OUTPUT_FILE: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
606 import os.path |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
607 for path in options: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
608 hdlr = logging.FileHandler(os.path.expanduser(path)) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
609 self._addHandler(root_logger, hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
610 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
611 raise ValueError("Unknown handler type") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
612 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
613 root_logger.warning(u"Handlers already set on root logger") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
614 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
615 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
616 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
|
617 """Configure logging behaviour |
991 | 618 @param backend: can be: |
619 C.LOG_BACKEND_STANDARD: use standard logging module | |
620 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
621 C.LOG_BACKEND_BASIC: use a basic print based logging | |
622 """ | |
623 global _backend | |
624 if _backend is not None: | |
625 raise exceptions.InternalError("Logging can only be configured once") | |
626 _backend = backend | |
627 | |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
628 if backend == C.LOG_BACKEND_BASIC: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
629 ConfigureBasic(**options) |
991 | 630 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
631 elif backend == C.LOG_BACKEND_TWISTED: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
632 ConfigureTwisted(**options) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
633 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
634 elif backend == C.LOG_BACKEND_STANDARD: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
635 ConfigureStandard(**options) |
991 | 636 |
637 else: | |
638 raise ValueError("unknown backend") | |
639 | |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
640 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
|
641 """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
|
642 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
643 @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
|
644 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
645 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
646 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
647 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
648 if COLORS in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
649 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
|
650 options[COLORS] = True |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
651 elif options[COLORS] == 'force': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
652 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
|
653 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
|
654 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
655 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
656 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
657 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
658 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
|
659 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
660 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
|
661 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
662 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None): |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
663 """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
|
664 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
665 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
666 @param backend: backend to use, it can be: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
667 - C.LOG_BACKEND_BASIC: print based backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
668 - C.LOG_BACKEND_TWISTED: Twisted logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
669 - C.LOG_BACKEND_STANDARD: standard logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
670 @param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values) |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
671 """ |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
672 if const is not None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
673 global C |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
674 C = const |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
675 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
|
676 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
|
677 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
|
678 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
|
679 config.read(C.CONFIG_FILES) |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
680 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
|
681 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
682 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
|
683 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
|
684 try: |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
685 log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name) |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
686 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
687 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
|
688 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
689 _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
|
690 configure(backend, **log_conf) |
991 | 691 |
692 def getLogger(name=C.LOG_BASE_LOGGER): | |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
693 if _backend in (None, C.LOG_BACKEND_BASIC): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
694 logger_class = Logger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
695 elif _backend == C.LOG_BACKEND_TWISTED: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
696 logger_class = TwistedLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
697 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
698 raise ValueError("This method should not be called with backend [%s]" % _backend) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
699 return _loggers.setdefault(name, logger_class(name)) |
991 | 700 |
701 _root_logger = getLogger() | |
702 | |
703 def debug(msg): | |
704 _root_logger.debug(msg) | |
705 | |
706 def info(msg): | |
707 _root_logger.info(msg) | |
708 | |
709 def warning(msg): | |
710 _root_logger.warning(msg) | |
711 | |
712 def error(msg): | |
713 _root_logger.error(msg) | |
714 | |
715 def critical(msg): | |
716 _root_logger.critical(msg) |