-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_helper.py
136 lines (109 loc) · 3.79 KB
/
logging_helper.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
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
''' Logging helper.
Just import this and your logger is configured to capture
both STDOUT and STDERR alongside logging.* calls to logs/output.log
this is not intended to be a library but a helper to configure logging
import from your main.py and then use logging.getLogger(...) normally
By default this helper rotates the log files at midnight.
set SELF_ROTATE_LOGS=False if your application uses external log rotations
'''
import logging
import logging.handlers
import os
import sys
FORMATTERS = {
'default': logging.Formatter(
'%(asctime)s %(levelname)s (%(name)s.%(funcName)s): %(message)s'
),
'lib': logging.Formatter(
'%(asctime)s %(levelname)s: %(filename)s:%(lineno)d %(funcName)s() - %(message)s'
),
'print': logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s'
),
}
PATH = 'logs'
LOGNAME = 'output.log'
if not os.path.isdir(PATH):
assert not os.path.exists(PATH)
os.mkdir(PATH)
SELF_ROTATE_LOGS = False
HANDLER_CONFIG = {
'filename': os.path.join(PATH, LOGNAME)
}
if SELF_ROTATE_LOGS:
HANDLER_CONFIG['when'] = 'midnight'
HandlerClass = logging.handlers.TimedRotatingFileHandler
else:
HandlerClass = logging.handlers.WatchedFileHandler
DEFAULT_HANDLER = HandlerClass(**HANDLER_CONFIG)
DEFAULT_HANDLER.setFormatter(FORMATTERS['default'])
DEFAULT_HANDLER.setLevel(logging.DEBUG)
class DefaultFilter:
@staticmethod
def filter(record):
return not hasattr(record, 'stdio')
DEFAULT_HANDLER.addFilter(DefaultFilter())
PRINTS_HANDLER = HandlerClass(**HANDLER_CONFIG)
PRINTS_HANDLER.setFormatter(FORMATTERS['print'])
PRINTS_HANDLER.setLevel(logging.DEBUG)
class StdioFilter:
@staticmethod
def filter(record):
return hasattr(record, 'stdio')
PRINTS_HANDLER.addFilter(StdioFilter())
HANDLERS = {
'default': DEFAULT_HANDLER,
'prints': PRINTS_HANDLER,
}
LOGGER = logging.getLogger(__name__)
LOGGER.root.setLevel(logging.DEBUG)
LOGGER.root.addHandler(HANDLERS['default'])
LOGGER.root.addHandler(HANDLERS['prints'])
class StreamToLogger:
'''
Fake file-like stream object that redirects writes to a logger instance.
credit: Ferry Boender
https://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
'''
def __init__(self, logger, file=None, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
self.file = file
# @contextmanager
# def switch_format(self):
# '''syntactic sugar to do the formatting handler replacement'''
# self.logger.root.removeHandler(HANDLERS['default'])
# self.logger.root.addHandler(HANDLERS['prints'])
# try:
# yield None
# finally:
# self.logger.root.removeHandler(HANDLERS['prints'])
# self.logger.root.addHandler(HANDLERS['default'])
def write(self, buf):
for line in buf.rstrip().splitlines():
line = line.strip()
if not line:
continue
self.logger.log(self.log_level, line, extra={'stdio': True})
if self.file:
print(line, file=self.file)
# def write(self, buf):
# '''write'''
# with self.switch_format():
# self._write(buf)
#
# self._write(buf)
def flush(self):
'''required for compliance'''
pass
STDOUT_LVL = logging.DEBUG + 1
STDOUT_LOGGER = logging.getLogger('STDOUT')
logging.addLevelName(STDOUT_LVL, 'STDOUT')
STREAM_LOGGER = StreamToLogger(STDOUT_LOGGER, sys.__stdout__, STDOUT_LVL)
sys.stdout = STREAM_LOGGER
STDERR_LVL = logging.DEBUG + 2
STDERR_LOGGER = logging.getLogger('STDERR')
logging.addLevelName(STDERR_LVL, 'STDERR')
STREAM_LOGGER = StreamToLogger(STDERR_LOGGER, sys.__stderr__, STDERR_LVL)
sys.stderr = STREAM_LOGGER