-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathstyle.py
96 lines (79 loc) · 3.14 KB
/
style.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
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
# coding=utf-8
"""Style Adapters for Python logging."""
from __future__ import unicode_literals
import collections
import functools
import logging
from six import text_type, viewitems
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class BraceMessage(object):
"""Lazily convert a Brace-formatted message."""
def __init__(self, msg, *args, **kwargs):
"""Initialize a lazy-formatted message."""
self.msg = msg
self.args = args
self.kwargs = kwargs
def __str__(self):
"""Convert to string."""
args = self.args
kwargs = self.kwargs
if args and len(args) == 1:
if args[0] and isinstance(args[0], collections.Mapping):
args = []
kwargs = self.args[0]
try:
try:
return self.msg.format(*args, **kwargs)
except IndexError:
try:
return self.msg.format(**kwargs)
except KeyError:
return self.msg
except KeyError:
try:
return self.msg.format(*args)
except KeyError:
raise Exception(self.msg)
except Exception:
log.exception(
'BraceMessage string formatting failed. '
'Using representation instead.'
)
return repr(self)
def __repr__(self):
"""Convert to class representation."""
sep = ', '
kw_repr = '{key}={value!r}'
name = self.__class__.__name__
args = sep.join(list(map(text_type, self.args)))
kwargs = sep.join(kw_repr.format(key=k, value=v)
for k, v in viewitems(self.kwargs))
return '{cls}({args})'.format(
cls=name,
args=sep.join([repr(self.msg), args, kwargs])
)
def format(self, *args, **kwargs):
"""Format a BraceMessage string."""
return text_type(self).format(*args, **kwargs)
class BraceAdapter(logging.LoggerAdapter):
"""Adapt logger to use Brace-formatted messages."""
def __init__(self, logger, extra=None):
"""Initialize the Brace adapter with a logger."""
super(BraceAdapter, self).__init__(logger, extra)
self.debug = functools.partial(self.log, logging.DEBUG)
self.info = functools.partial(self.log, logging.INFO)
self.warning = functools.partial(self.log, logging.WARNING)
self.error = functools.partial(self.log, logging.ERROR)
self.critical = functools.partial(self.log, logging.CRITICAL)
def log(self, level, msg, *args, **kwargs):
"""Log a message at the specified level using Brace-formatting."""
if self.isEnabledFor(level):
msg, kwargs = self.process(msg, kwargs)
if not isinstance(msg, BraceMessage):
msg = BraceMessage(msg, *args, **kwargs)
self.logger.log(level, msg, **kwargs)
def exception(self, msg, *args, **kwargs):
"""Add exception information before delegating to self.log."""
kwargs['exc_info'] = 1
self.log(logging.ERROR, msg, *args, **kwargs)