-
Notifications
You must be signed in to change notification settings - Fork 1
/
L2A_Logger.py
127 lines (106 loc) · 4.08 KB
/
L2A_Logger.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
#!/usr/bin/env python
import os
import sys
import time
import traceback
import multiprocessing, threading, logging, sys
from logging.handlers import RotatingFileHandler
DEFAULT_LEVEL = logging.INFO
STREAM = 25
FATAL = 50
logging.addLevelName(STREAM, " INFO")
logging.addLevelName(FATAL, "FATAL")
Logger = logging.getLoggerClass()
class SubProcessLogHandler(logging.Handler):
def __init__(self, queue):
logging.Handler.__init__(self)
self.queue = queue
def emit(self, record):
self.queue.put(record)
class LogQueueReader(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.daemon = True
def run(self):
while True:
try:
record = self.queue.get()
logger = logging.getLogger(record.name)
logger.callHandlers(record)
except (KeyboardInterrupt, SystemExit):
raise
except EOFError:
break
except:
traceback.print_exc(file=sys.stderr)
class UTCFormatter(logging.Formatter):
converter = time.gmtime
def formatTime(self, record, datefmt=None):
ct = self.converter(record.created)
if datefmt:
s = time.strftime(datefmt, ct)
else:
t = time.strftime("%Y-%m-%dT%H:%M:%S", ct)
s = "%s.%03d" % (t, record.msecs)
return s
class L2A_Logger(Logger):
def __init__(self, name, fnLog = None, logLevel = DEFAULT_LEVEL, operation_mode = 'TOOLBOX'):
Logger.__init__(self, name)
self._logLevel = logLevel
self._strmLevel = ' INFO'
self._fnLog = fnLog
if operation_mode == 'TOOLBOX' or operation_mode == None:
if self._strmLevel == 'DEBUG':
formatter = UTCFormatter(' %(module).12s : %(lineno)s %(message)s')
else:
formatter = UTCFormatter('%(message)s')
else:
if self._strmLevel == 'DEBUG':
format = '%(asctime)s ' + os.uname()[1] +' %(process).6d %(module).13s: %(lineno)s [%(levelname)5s] %(message)s'
else:
format = '%(asctime)s ' + os.uname()[1] +' %(process).6d [%(levelname)5s] %(message)s'
formatter = UTCFormatter(fmt=format)
strm = logging.StreamHandler(sys.stdout)
strm.setLevel(self._strmLevel)
strm.setFormatter(formatter)
self.addHandler(strm)
if fnLog:
format = '<check>\n<inspection execution=\"%(asctime)s\" level=\"%(levelname)s\" process=\"%(process)d\" module=\"%(module)s\" function=\"%(funcName)s\" line=\"%(lineno)d\"/>\n<message contentType=\"Text\">%(message)s</message>\n</check>'
formatter = UTCFormatter(fmt=format)
handler = logging.FileHandler(self._fnLog,'a')
handler.setFormatter(formatter)
handler.setLevel(self._logLevel)
self.addHandler(handler)
self.level = getLevel(self._logLevel)
if self._fnLog:
self.info('logging for the main process initialized')
def get_fn_log(self):
return self._fnLog
def set_fn_log(self, value):
self._fnLog = value
def del_fn_log(self):
del self._fnLog
fnLog = property(get_fn_log, set_fn_log, del_fn_log, "fnLog's docstring")
def stream(self, msg, *args, **kwargs):
"""
Log 'msg % args' with severity 'STREAM'.
To pass exception information, use the keyword argument exc_info with
a true value, e.g.
logger.stream("Houston, we have a %s", "interesting problem", exc_info=1)
"""
if self.isEnabledFor(STREAM):
self._log(STREAM, msg, args, **kwargs)
def getLevel(level):
if level == 'DEBUG':
return logging.DEBUG
elif level == 'INFO':
return logging.INFO
elif level == 'WARNING':
logging.WARNING
elif level == 'ERROR':
return logging.ERROR
elif level == 'CRITICAL':
return logging.CRITICAL
else:
return logging.NOTSET