This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlogdecorator.py
67 lines (52 loc) · 2.55 KB
/
logdecorator.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import re
import sys
import traceback
class LogDecorator(object):
def __init__(self, logger, extradict, extraformat):
self._logger = logger
self._extradict = extradict
self._extraformat = extraformat
if re.search('(%[(][a-zA-Z]+[)][^a-z]|%[(][a-zA-Z]+[)]$)', extraformat):
raise ValueError('format string contains a %(attribute)'
'pattern without a type specifier.')
def clone(self, extradict=None, extraformat=None):
extradict = extradict or self._extradict
extraformat = extraformat or self._extraformat
return LogDecorator(self._logger, extradict, extraformat)
def _expanded_message(self, message):
extradict = dict(self._extradict)
try:
if isinstance(message, unicode):
extradict['message'] = message
else:
extradict['message'] = unicode(message, errors='replace')
extramessage = self._extraformat % extradict
except:
etype, evalue, etraceback = sys.exc_info()
extramessage = ''.join(traceback.format_exception(etype, evalue, etraceback))
print extramessage
print message
return extramessage
def logger(self):
return self._logger
def getEffectiveLevel(self):
return self._logger.getEffectiveLevel()
def debug(self, message, *args, **kwargs):
self._logger.debug(self._expanded_message(message), *args, **kwargs)
def info(self, message, *args, **kwargs):
self._logger.info(self._expanded_message(message), *args, **kwargs)
def warning(self, message, *args, **kwargs):
self._logger.warning(self._expanded_message(message), *args, **kwargs)
def warn(self, message, *args, **kwargs):
self._logger.warning(self._expanded_message(message), *args, **kwargs)
def error(self, message, *args, **kwargs):
self._logger.error(self._expanded_message(message), *args, **kwargs)
def critical(self, message, *args, **kwargs):
self._logger.critical(self._expanded_message(message), *args, **kwargs)
def log(self, lvl, message, *args, **kwargs):
self._logger.log(lvl, self._expanded_message(message), *args, **kwargs)
def exception(self, message, *args, **kwargs):
self._logger.exception(self._expanded_message(message), *args, **kwargs)