Mercurial > libervia-backend
annotate sat/core/log.py @ 2833:ea5fcbcfc8e8
Added tag 0.7.0a4 for changeset 1798d4ace296
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Mar 2019 19:48:25 +0100 |
parents | 003b8b4b56a7 |
children | ad00f61fd9f5 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1 #!/usr/bin/env python2 |
991 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SàT: a XMPP client | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
991 | 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. |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3 |
991 | 23 |
24 from sat.core.constants import Const as C | |
2154
7cbffd754b4a
core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
25 from sat.tools.common.ansi import ANSI as A |
991 | 26 from sat.core import exceptions |
27 | |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
28 backend = None |
991 | 29 _loggers = {} |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
30 handlers = {} |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
31 COLOR_START = '%(color_start)s' |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
32 COLOR_END = '%(color_end)s' |
991 | 33 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
34 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
35 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
|
36 pass |
991 | 37 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
38 |
991 | 39 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
|
40 """High level logging class""" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
41 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
|
42 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
|
43 post_treat = None |
991 | 44 |
45 def __init__(self, name): | |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
46 if isinstance(name, Logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
47 self.copy(name) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
48 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
49 self._name = name |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
50 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
51 def copy(self, other): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
52 """Copy values from other Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
53 self.fmt = other.fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
54 self.Filter_name = other.fmt |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
55 self.post_treat = other.post_treat |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
56 self._name = other._name |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
57 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
58 def out(self, message, level=None): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
59 """Actually log the message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
60 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
61 @param message: formatted message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
62 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
63 print message |
991 | 64 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
65 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
|
66 """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
|
67 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
68 @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
|
69 @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
|
70 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
71 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
72 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
|
73 if self.post_treat is None: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
74 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
|
75 else: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
76 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
|
77 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
|
78 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
79 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
80 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
|
81 """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
|
82 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
83 @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
|
84 @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
|
85 @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
|
86 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
87 @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
|
88 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 '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
|
93 '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
|
94 } |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
95 try: |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
96 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
|
97 raise Filtered |
1016
0c361fdc76af
core (logs): workaround for pyjamas bug
Goffi <goffi@goffi.org>
parents:
1013
diff
changeset
|
98 except (AttributeError, TypeError): # XXX: TypeError is here because of a pyjamas bug which need to be fixed (TypeError is raised instead of AttributeError) |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
99 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
|
100 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
|
101 try: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
102 return self.fmt % record |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
103 except TypeError: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
104 return message |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
105 except KeyError as e: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
106 if e.args[0] == 'profile': |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
107 # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
108 record['profile'] = configure_cls[backend].getProfile() |
1008
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
109 return self.fmt % record |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
110 else: |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
111 raise e |
d70d4fe5c5f8
core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents:
1007
diff
changeset
|
112 |
991 | 113 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
|
114 self.log(C.LOG_LVL_DEBUG, msg) |
991 | 115 |
116 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
|
117 self.log(C.LOG_LVL_INFO, msg) |
991 | 118 |
119 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
|
120 self.log(C.LOG_LVL_WARNING, msg) |
991 | 121 |
122 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
|
123 self.log(C.LOG_LVL_ERROR, msg) |
991 | 124 |
125 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
|
126 self.log(C.LOG_LVL_CRITICAL, msg) |
991 | 127 |
128 | |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
129 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
|
130 """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
|
131 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
132 def __init__(self, name_re): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
133 """Initialise name filter |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
134 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
135 @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
|
136 """ |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
137 assert name_re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
138 import re |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
139 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
|
140 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
141 def filter(self, record): |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
142 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
|
143 return 1 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
144 return 0 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
145 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
146 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
|
147 """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
|
148 |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
149 @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
|
150 @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
|
151 """ |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
152 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
|
153 pass |
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
158 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
159 class ConfigureBase(object): |
1017
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
160 LOGGER_CLASS = Logger |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
161 # True if color location is specified in fmt (with COLOR_START) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 _color_location = False |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
163 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 levels_taints_dict=None, force_colors=False, backend_data=None): |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
166 """Configure a backend |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
167 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
168 @param level: one of C.LOG_LEVELS |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 @param fmt: format string, pretty much as in std logging. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 Accept the following keywords (maybe more depending on backend): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
171 - "message" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
172 - "levelname" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
173 - "name" (logger name) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
174 @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
|
175 Use search to match expression, so ^ or $ can be necessary. |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
176 @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
|
177 @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
|
178 """ |
1129
08f50fdac21b
core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents:
1021
diff
changeset
|
179 self.backend_data = backend_data |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
180 self.preTreatment() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
181 self.configureLevel(level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
182 self.configureFormat(fmt) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
183 self.configureOutput(output) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
184 self.configureLogger(logger) |
1942 | 185 self.configureColors(colors, force_colors, levels_taints_dict) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
186 self.postTreatment() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
187 self.updateCurrentLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
188 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
189 def updateCurrentLogger(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
190 """update existing logger to the class needed for this backend""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
191 if self.LOGGER_CLASS is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
192 return |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
193 for name, logger in _loggers.items(): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
194 _loggers[name] = self.LOGGER_CLASS(logger) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
195 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
196 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
197 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
198 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
199 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
200 if level is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
201 # we deactivate methods below level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
202 level_idx = C.LOG_LEVELS.index(level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
203 def dev_null(self, msg): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
204 pass |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
205 for _level in C.LOG_LEVELS[:level_idx]: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
206 setattr(Logger, _level.lower(), dev_null) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
207 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
208 def configureFormat(self, fmt): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
209 if fmt is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
210 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
|
211 Logger.fmt = fmt |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
212 if COLOR_START in fmt: |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
213 ConfigureBase._color_location = True |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
214 if fmt.find(COLOR_END,fmt.rfind(COLOR_START))<0: |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
215 # color_start not followed by an end, we add it |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
216 Logger.fmt += COLOR_END |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
217 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
218 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
219 if output is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
220 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
|
221 # TODO: manage other outputs |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
222 raise NotImplementedError("Basic backend only manage default output yet") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
223 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
224 def configureLogger(self, logger): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
225 if logger: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
226 Logger.filter_name = FilterName(logger) |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
227 |
1942 | 228 def configureColors(self, colors, force_colors, levels_taints_dict): |
229 if colors: | |
230 # if color are used, we need to handle levels_taints_dict | |
231 for level in levels_taints_dict.keys(): | |
232 # we wants levels in uppercase to correspond to contstants | |
233 levels_taints_dict[level.upper()] = levels_taints_dict[level] | |
234 taints = self.__class__.taints = {} | |
235 for level in C.LOG_LEVELS: | |
236 # we want use values and use constant value as default | |
237 taint_list = levels_taints_dict.get(level, C.LOG_OPT_TAINTS_DICT[1][level]) | |
238 ansi_list = [] | |
239 for elt in taint_list: | |
240 elt = elt.upper() | |
241 try: | |
2154
7cbffd754b4a
core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
242 ansi = getattr(A, 'FG_{}'.format(elt)) |
1942 | 243 except AttributeError: |
244 try: | |
2154
7cbffd754b4a
core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
245 ansi = getattr(A, elt) |
1942 | 246 except AttributeError: |
247 # we use raw string if element is unknown | |
248 ansi = elt | |
249 ansi_list.append(ansi) | |
250 taints[level] = ''.join(ansi_list) | |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
251 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
252 def postTreatment(self): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
253 pass |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
254 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
255 def manageOutputs(self, outputs_raw): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
256 """ Parse output option in a backend agnostic way, and fill handlers consequently |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
257 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
258 @param outputs_raw: output option as enterred in environment variable or in configuration |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
259 """ |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
260 if not outputs_raw: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
261 return |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
262 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP) |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
263 global handlers |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
264 if len(outputs) == 1: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
265 handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()] |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
266 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
267 for output in outputs: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
268 if not output: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
269 continue |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
270 if output[-1] == ')': |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
271 # we have options |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
272 opt_begin = output.rfind('(') |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
273 options = output[opt_begin+1:-1] |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
274 output = output[:opt_begin] |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
275 else: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
276 options = None |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
277 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
278 if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
279 raise ValueError(u"Invalid output [%s]" % output) |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
280 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
281 if output == C.LOG_OPT_OUTPUT_DEFAULT: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
282 # no option for defaut handler |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
283 handlers[output] = None |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
284 elif output == C.LOG_OPT_OUTPUT_FILE: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
285 if not options: |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
286 ValueError("{handler} output need a path as option" .format(handle=output)) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
287 handlers.setdefault(output, []).append(options) |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
288 options = None # option are parsed, we can empty them |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
289 elif output == C.LOG_OPT_OUTPUT_MEMORY: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
290 # we have memory handler, option can be the len limit or None |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
291 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
292 limit = int(options) |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
293 options = None # option are parsed, we can empty them |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
294 except (TypeError, ValueError): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
295 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
296 handlers[output] = limit |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
297 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
298 if options: # we should not have unparsed options |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
299 raise ValueError(u"options [{options}] are not supported for {handler} output".format(options=options, handler=output)) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
300 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
301 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
302 def memoryGet(size=None): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
303 """Return buffered logs |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
304 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
305 @param size: number of logs to return |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
306 """ |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
307 raise NotImplementedError |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
308 |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
309 @classmethod |
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
310 def ansiColors(cls, level, message): |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
311 """Colorise message depending on level for terminals |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
312 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
313 @param level: one of C.LOG_LEVELS |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
314 @param message: formatted message to log |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
315 @return: message with ANSI escape codes for coloration |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
316 """ |
1942 | 317 |
318 try: | |
319 start = cls.taints[level] | |
320 except KeyError: | |
321 start = '' | |
322 | |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
323 if cls._color_location: |
1942 | 324 return message % {'color_start': start, |
2154
7cbffd754b4a
core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
325 'color_end': A.RESET} |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
326 else: |
2154
7cbffd754b4a
core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
327 return '%s%s%s' % (start, message, A.RESET) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
328 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
329 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
330 def getProfile(): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
331 """Try to find profile value using introspection""" |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
332 raise NotImplementedError |
1007
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 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
335 class ConfigureCustom(ConfigureBase): |
1017
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
336 LOGGER_CLASS = None |
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
337 |
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
338 def __init__(self, logger_class, *args, **kwargs): |
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
339 ConfigureCustom.LOGGER_CLASS = logger_class |
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
340 |
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
341 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
342 configure_cls = { None: ConfigureBase, |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
343 C.LOG_BACKEND_CUSTOM: ConfigureCustom |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
344 } # XXX: (key: backend, value: Configure subclass) must be filled when new backend are added |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
345 |
1006
325fd230c15d
core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents:
1005
diff
changeset
|
346 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
347 def configure(backend_, **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
|
348 """Configure logging behaviour |
991 | 349 @param backend: can be: |
350 C.LOG_BACKEND_BASIC: use a basic print based logging | |
1017
0ea97f483464
core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents:
1016
diff
changeset
|
351 C.LOG_BACKEND_CUSTOM: use a given Logger subclass |
991 | 352 """ |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
353 global backend |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
354 if backend is not None: |
991 | 355 raise exceptions.InternalError("Logging can only be configured once") |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
356 backend = backend_ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
357 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
358 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
359 configure_class = configure_cls[backend] |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
360 except KeyError: |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
361 raise ValueError("unknown backend [{}]".format(backend)) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
362 if backend == C.LOG_BACKEND_CUSTOM: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
363 logger_class = options.pop('logger_class') |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
364 configure_class(logger_class, **options) |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
365 else: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
366 configure_class(**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
|
367 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
368 def memoryGet(size=None): |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
369 if not C.LOG_OPT_OUTPUT_MEMORY in handlers: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
370 raise ValueError('memory output is not used') |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
371 return configure_cls[backend].memoryGet(size) |
991 | 372 |
373 def getLogger(name=C.LOG_BASE_LOGGER): | |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
374 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
375 logger_class = configure_cls[backend].LOGGER_CLASS |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
376 except KeyError: |
1940
3fdacba9da68
core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
377 raise ValueError("This method should not be called with backend [{}]".format(backend)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
378 return _loggers.setdefault(name, logger_class(name)) |
991 | 379 |
380 _root_logger = getLogger() | |
381 | |
382 def debug(msg): | |
383 _root_logger.debug(msg) | |
384 | |
385 def info(msg): | |
386 _root_logger.info(msg) | |
387 | |
388 def warning(msg): | |
389 _root_logger.warning(msg) | |
390 | |
391 def error(msg): | |
392 _root_logger.error(msg) | |
393 | |
394 def critical(msg): | |
395 _root_logger.critical(msg) |