-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
62 lines (51 loc) · 1.75 KB
/
__init__.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
import collections
import logging
__title__ = 'logaugment'
__version__ = '0.1.3'
__author__ = 'Simeon Visser'
__email__ = 'simeonvisser@gmail.com'
__license__ = 'MIT'
__copyright__ = 'Copyright 2014 Simeon Visser'
class AugmentFilter(logging.Filter):
def __init__(self, name='', args=None):
super(AugmentFilter, self).__init__(name)
self._args = args
def filter(self, record):
if self._args is not None:
data = {}
try:
data = self._args(record)
except TypeError:
pass
if not data and isinstance(self._args, collections.Mapping):
data = self._args
if data and not hasattr(record, '_logaugment'):
record._logaugment = {}
for key, value in data.items():
if (record.__dict__.get(key) is None or
key in record._logaugment):
setattr(record, key, value)
record._logaugment[key] = value
return True
def add(logger, *args, **kwargs):
if not args and not kwargs:
raise ValueError("You need to provide at least one "
"custom key or function")
argument = None
if len(args) == 1 and not kwargs:
argument = args[0]
elif not args and kwargs:
argument = kwargs
logger.addFilter(
AugmentFilter(name='logaugment.AugmentFilter', args=argument)
)
def reset(logger):
remove = []
for filter_obj in logger.filters:
if isinstance(filter_obj, AugmentFilter):
remove.append(filter_obj)
for remove_obj in remove:
logger.removeFilter(remove_obj)
def set(logger, *args, **kwargs):
reset(logger)
add(logger, *args, **kwargs)