-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixLogging.py
92 lines (83 loc) · 3.43 KB
/
fixLogging.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
#Discord ProtOS Bot
#
#Author: Jascha "fredi_68" Hirsekorn
#
#Monkey patches the logging module to enable
#dictConfig() to deal with positional arguments.
#Cause whoever programmed that library was NOT aware,
#that some handlers need that.
import logging
from logging import config
print("[LogArgFix] Patching position argument support...")
class DictConfigurator(config.DictConfigurator):
def configure_handler(self, config):
"""
Configure a handler from a dictionary.
"""
config_copy = dict(config) # for restoring in case of error
formatter = config.pop('formatter', None)
if formatter:
try:
formatter = self.config['formatters'][formatter]
except Exception as e:
raise ValueError('Unable to set formatter '
'%r: %s' % (formatter, e))
level = config.pop('level', None)
filters = config.pop('filters', None)
if '()' in config:
c = config.pop('()')
if not callable(c):
c = self.resolve(c)
factory = c
else:
cname = config.pop('class')
klass = self.resolve(cname)
#Special case for handler which refers to another handler
if issubclass(klass, logging.handlers.MemoryHandler) and\
'target' in config:
try:
th = self.config['handlers'][config['target']]
if not isinstance(th, logging.Handler):
config.update(config_copy) # restore for deferred cfg
raise TypeError('target not configured yet')
config['target'] = th
except Exception as e:
raise ValueError('Unable to set target handler '
'%r: %s' % (config['target'], e))
elif issubclass(klass, logging.handlers.SMTPHandler) and\
'mailhost' in config:
config['mailhost'] = self.as_tuple(config['mailhost'])
elif issubclass(klass, logging.handlers.SysLogHandler) and\
'address' in config:
config['address'] = self.as_tuple(config['address'])
factory = klass
#Enable position arguments
args = []
if "args" in config:
args = config["args"]
del config["args"]
props = config.pop('.', None)
kwargs = dict([(k, config[k]) for k in config if logging.config.valid_ident(k)])
try:
result = factory(*args, **kwargs) #initialize with positional arguments
except TypeError as te:
if "'stream'" not in str(te):
raise
#The argument name changed from strm to stream
#Retry with old name.
#This is so that code can be used with older Python versions
#(e.g. by Django)
kwargs['strm'] = kwargs.pop('stream')
result = factory(**kwargs)
if formatter:
result.setFormatter(formatter)
if level is not None:
result.setLevel(logging._checkLevel(level))
if filters:
self.add_filters(result, filters)
if props:
for name, value in props.items():
setattr(result, name, value)
return result
logging.config.dictConfigClass = DictConfigurator
print("[LogArgFix] Logging module patch successfull")