-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_logging.py
More file actions
318 lines (257 loc) · 9.33 KB
/
_logging.py
File metadata and controls
318 lines (257 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
pyDKB.common._logging
A wrapper for standard 'logging' module.
"""
import logging
from logging import CRITICAL, FATAL, ERROR, WARNING, WARN, INFO, DEBUG, NOTSET
import sys
__logging_version = int(logging.__version__.split('.')[1])
# ---------------------------------------------------
# Module variables
# ---------------------------------------------------
#
# Some variables are initialized in 'init()' function.
#
# Logger instance to be used in the module
logger = None
# Root logger for the whole library
_rootLogger = None
# Additional log level
TRACE = DEBUG / 2
logging.addLevelName(TRACE, 'TRACE')
# -------------------------------------------
# Custom implementation for 'logging' classes
# -------------------------------------------
class Logger(logging.Logger, object):
""" Logger implementation, aware of 'TRACE' log level.
New methods:
* trace() -- log with TRACE level;
* traceback() -- log traceback with DEBUG level.
"""
def trace(self, msg, *args, **kwargs):
""" Log 'msg % args' with severity 'TRACE'.
To pass exception information, use the keyword argument exc_info with
a true value, e.g.
logger.trace("Houston, we have a %s", "interesting problem",
exc_info=1)
"""
if self.isEnabledFor(TRACE):
self._log(TRACE, msg, args, **kwargs)
def traceback(self, **kwargs):
""" Log traceback without additionat messages with severity 'DEBUG'.
logger.traceback()
"""
if self.isEnabledFor(DEBUG):
if not (kwargs.get('exc_info')):
kwargs['exc_info'] = 1
self.debug('Traceback info:', **kwargs)
class RootLogger(Logger):
""" Same as Logger, but must must have `Level` and be the only one. """
def __init__(self, level, name='root'):
""" Initialize new root logger. """
Logger.__init__(self, name, level)
class MultilineFormatter(logging.Formatter, object):
""" Formatter for multiline messages.
Every extra line (directly in the message body, or the traceback)
is:
* prefixed with ' (==)';
* suffixed with the part of format string which goes after
'%(message)s' part.
"""
_suffix = None
def __init__(self, *args):
""" Split format string into message format and suffix. """
new_args = list(args)
if len(args):
fmt = args[0]
new_args[0], self._suffix = self.splitFormat(fmt)
super(MultilineFormatter, self).__init__(*new_args)
def splitFormat(self, fmt):
""" Split format string into msg format and suffix.
Suffix is everything that goes after the message body.
"""
format, suffix = ('', '')
splitted = fmt.split("%(message)s")
if len(splitted) > 1:
format = "%(message)s".join(splitted[:-1]) + "%(message)s"
suffix = splitted[-1]
else:
format = fmt
return format, suffix
def formatSuffix(self, record):
""" Return formatted suffix. """
return self._suffix % record.__dict__
def formatExtra(self, lines, prefix=" (==) ", suffix='', align=False):
""" Format extra lines of the log message (traceback, ...).
Parameter 'align' shows whether the suffix should be aligned
to the right (by the longest line), or to the left (as for normal
log messages).
"""
if isinstance(lines, list) and len(lines):
max_len = len(max(lines, key=len))
# Need to add prefix len (in case that the longest line is
# among those that will be prefixed).
max_len += len(prefix)
if suffix and align:
line_fmt = "%%(line)-%ds" % max_len
else:
line_fmt = "%(line)s"
extra = line_fmt % {'line': lines[0]} + suffix
for line in lines[1:]:
extra += "\n" + line_fmt % {'line': prefix + line} + suffix
else:
extra = ""
return extra
def format(self, record):
""" Format multiline message.
Second and further lines from initial message are formatted
'extra' lines.
"""
formatted = super(MultilineFormatter, self).format(record)
lines = formatted.splitlines()
suffix = self.formatSuffix(record)
result = self.formatExtra(lines, suffix=suffix, align=True)
return result
# --------------------------
# Module top-level functions
# --------------------------
def isInitialized():
""" Checks if the module (namely, root logger) is initialized. """
return isinstance(_rootLogger, Logger) and _rootLogger.handlers
def getRootLogger(**kwargs):
""" Reconfigure and return library root logger. """
if kwargs or not isInitialized():
configureRootLogger(**kwargs)
return _rootLogger
def init(**kwargs):
""" Initialize module.
If already initialized, do nothing.
"""
global logger
if not isInitialized():
initRootLogger(**kwargs)
logger = getLogger(__name__)
def initRootLogger(**kwargs):
""" Initialize root logger.
If already initialized, do nothing.
"""
global _rootLogger
if isInitialized():
return _rootLogger
name = kwargs.get('name')
if not name:
name = __name__.split('.')[0]
# Create new root logger
root = RootLogger(WARNING, name)
# Set Logger class 'root' to the new root
Logger.root = root
# Create new manager (object, responsible for new loggers creation)
manager = logging.Manager(root)
if __logging_version < 5:
logging.setLoggerClass(Logger)
else:
manager.setLoggerClass(Logger)
Logger.manager = manager
root.manager = Logger.manager
# 3) reset root logger for our class.
Logger.root = root
filename = kwargs.get('filename')
if filename:
mode = kwargs.get('mode', 'a')
hdlr_name = "file_%s_%s" % (filename, mode)
# Open file without delay, as presence of 'filename'
# keyword supposes that function was called during
# intended module initialization, not as a side effect
# of calling 'getLogger()' before initializtion.
hdlr = FileHandler(filename, mode)
else:
stream = kwargs.get('stream', sys.stderr)
hdlr_name = "stream_%s" % stream.fileno()
hdlr = logging.StreamHandler(stream)
if __logging_version < 5:
hdlr._name = hdlr_name
else:
hdlr.set_name(hdlr_name)
fs = kwargs.get('msg_format', logging.BASIC_FORMAT)
dfs = kwargs.get('datefmt', None)
fmt = MultilineFormatter(fs, dfs)
hdlr.setFormatter(fmt)
root.addHandler(hdlr)
level = kwargs.get('level')
if level is not None:
root.setLevel(level)
_rootLogger = root
return _rootLogger
def configureRootLogger(**kwargs):
""" (Re)configure root logger. """
if not isInitialized():
return init(**kwargs)
root = _rootLogger
name = kwargs.get('name')
if name and name != root.name:
# Renaming is not allowed,
# as logger name is the hierarchy-forming parameter
logger.warning("Root logger ('%s') can not be renamed to '%s'.",
root.name, name)
# Root logger is supposed to have exactly one handler,
# so we count on this fact here
old_hdlr = root.handlers[0]
old_fmt = old_hdlr.formatter
filename = kwargs.get('filename')
stream = kwargs.get('stream', sys.stderr)
if filename:
mode = kwargs.get('mode', 'a')
hdlr_name = "file_%s_%s" % (filename, mode)
# Delay allows us not to open file right now,
# but only when it is actually required
hdlr = FileHandler(filename, mode, delay=True)
elif stream:
hdlr_name = "stream_%s" % stream.fileno()
hdlr = logging.StreamHandler(stream)
else:
hdlr = old_hdlr
if __logging_version < 5:
if not getattr(hdlr, '_name', None):
hdlr._name = hdlr_name
else:
if not hdlr.get_name():
hdlr.set_name(hdlr_name)
# Remove old handler or go on with it instead of the newly created one
# (if it is configured just the same way).
if old_hdlr != hdlr:
if __logging_version < 5 \
and (getattr(old_hdlr, '_name', None)
!= getattr(hdlr, '_name', '')) \
or __logging_version >= 5 \
and old_hdlr.get_name() != hdlr.get_name():
old_hdlr.close()
root.removeHandler(old_hdlr)
root.addHandler(hdlr)
else:
hdlr.close()
hdlr = old_hdlr
fs = kwargs.get("msg_format")
dfs = kwargs.get("datefmt")
# Check if handler formatter was configured earlier
# and use old values if no new specified
if isinstance(old_fmt, logging.Formatter):
if not fs:
fs = old_fmt._fmt
if not dfs:
dfs = old_fmt.datefmt
elif not fs:
fs = logging.BASIC_FORMAT
fmt = MultilineFormatter(fs, dfs)
hdlr.setFormatter(fmt)
level = kwargs.get("level")
if level is not None:
root.setLevel(level)
return root
def getLogger(name):
""" Return logger with given name. """
if not isInitialized():
init()
root = _rootLogger
if name == root.name:
return root
return root.manager.getLogger(name)