/
rcLogger.py
251 lines (217 loc) · 7.58 KB
/
rcLogger.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import sys
import os
import gzip
import errno
import logging
import logging.handlers
import six
from rcGlobalEnv import rcEnv
from rcUtilities import makedirs
from subprocess import *
min_name_len = 10
namelen = 10
namefmt = "%-"+str(namelen)+"s"
try:
type(PermissionError)
except:
PermissionError = IOError
import platform
import re
import extconfig
from rcColor import colorize, color
DEFAULT_HANDLERS = ["file", "stream", "syslog"]
def namer(name):
return name + ".gz"
def rotator(source, dest):
with open(source, "rb") as sf:
data = sf.read()
with gzip.open(dest, "wb") as df:
df.write(data)
os.remove(source)
class StreamFilter(logging.Filter):
def filter(self, record):
try:
if record.args["f_stream"] is False:
return False
else:
return True
except (KeyError, AttributeError, TypeError):
return True
class RedactingFormatter(object):
def __init__(self, orig_formatter):
self.orig_formatter = orig_formatter
def format(self, record):
msg = self.orig_formatter.format(record)
for pattern in extconfig.SECRETS:
msg = msg.replace(pattern, "xxxx")
return msg
def __getattr__(self, attr):
return getattr(self.orig_formatter, attr)
class ColorStreamHandler(logging.StreamHandler):
def __init__(self, stream=None):
logging.StreamHandler.__init__(self, stream)
def format(self, record):
text = logging.StreamHandler.format(self, record)
def c(line):
l = line.rstrip("\n").split(" - ")
if len(l) < 3:
return line
l[0] = namefmt % l[0]
l[0] = colorize(l[0], color.BOLD)
l[1] = l[1].replace("ERROR", colorize("E", color.RED))
l[1] = l[1].replace("WARNING", colorize("W", color.BROWN))
l[1] = l[1].replace("DEBUG", colorize("D", color.LIGHTBLUE))
l[1] = l[1].replace("INFO", " ")
if l[2].startswith("do "):
l[2] = colorize(l[2], color.BOLD)
return " ".join(l)
return c(text)
class LoggerHandler(logging.handlers.SysLogHandler):
def __init__(self, facility=logging.handlers.SysLogHandler.LOG_USER):
logging.Handler.__init__(self)
self.formatter = None
self.facility = facility.upper()
def close(self):
pass
def emit(self, record):
"""
Emit a record.
The record is formatted, and then sent to the syslog server. If
exception information is present, it is NOT sent to the server.
"""
try:
import syslog
facility = syslog.__dict__["LOG_"+self.facility]
syslog.openlog("opensvc", 0, facility)
except Exception as exc:
self.handleError(record)
try:
prio = self.priority_names[self.mapPriority(record.levelname)]
msg = self.format(record)
if six.PY2:
msg = msg.encode("utf8", errors="ignore")
syslog.syslog(prio, msg)
except Exception as exc:
self.handleError(record)
finally:
syslog.closelog()
def set_namelen(svcs=[], force=None):
global namelen
global namefmt
maxlen = min_name_len
for svc in svcs:
if svc.disabled:
continue
svc_len = len(svc.name)
# init with the "scheduler" length
max_res_len = 10
for r in svc.resources_by_id.values():
if r is None:
continue
if r.is_disabled():
continue
l = len(r.rid)
if r.subset:
l += len(r.subset) + 2
if l > max_res_len:
max_res_len = l
svc_len += max_res_len
if svc_len > maxlen:
maxlen = svc_len
maxlen += len(rcEnv.nodename) + 1
if force:
maxlen = force
namelen = maxlen
namefmt = "%-"+str(namelen)+"s"
def initLogger(root, logfile, handlers=None):
if handlers is None:
handlers = DEFAULT_HANDLERS
log = logging.getLogger(root)
log.propagate = False
log.handlers = []
if "file" in handlers:
logdir = os.path.dirname(logfile)
makedirs(logdir)
try:
fileformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
filehandler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=1*5242880,
backupCount=1)
filehandler.setFormatter(RedactingFormatter(fileformatter))
filehandler.setLevel(logging.INFO)
filehandler.rotator = rotator
filehandler.namer = namer
log.addHandler(filehandler)
if '--debug' in sys.argv:
filehandler.setLevel(logging.DEBUG)
except PermissionError:
pass
if "stream" in handlers:
streamformatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
streamhandler = ColorStreamHandler()
streamhandler.setFormatter(RedactingFormatter(streamformatter))
streamfilter = StreamFilter()
streamhandler.addFilter(streamfilter)
log.addHandler(streamhandler)
if '--debug' in sys.argv:
rcEnv.loglevel = logging.DEBUG
streamhandler.setLevel(logging.DEBUG)
elif '--warn' in sys.argv:
rcEnv.loglevel = logging.WARNING
streamhandler.setLevel(logging.WARNING)
elif '--error' in sys.argv:
rcEnv.loglevel = logging.ERROR
streamhandler.setLevel(logging.ERROR)
else:
rcEnv.loglevel = logging.INFO
streamhandler.setLevel(logging.INFO)
if "syslog" in handlers:
from six.moves import configparser as ConfigParser
config = ConfigParser.RawConfigParser({})
try:
config.read(rcEnv.paths.nodeconf)
except:
pass
try:
facility = config.get("syslog", "facility")
except:
facility = "daemon"
try:
lvl = config.get("syslog", "level").upper()
lvl = getattr(logging, lvl)
except:
lvl = logging.INFO
try:
host = config.get("syslog", "host")
except:
host = None
try:
port = int(config.get("syslog", "port"))
except:
port = None
address = None
if host is None and port is None:
if os.path.exists("/dev/log"):
address = os.path.realpath("/dev/log")
elif os.path.exists("/var/run/syslog"):
address = os.path.realpath("/var/run/syslog")
if address is None:
if host is None:
host = "localhost"
if port is None:
port = 514
address = (host, port)
syslogformatter = logging.Formatter("%(name)s %(message)s")
try:
if rcEnv.sysname == "SunOS" and not isinstance(address, tuple):
sysloghandler = LoggerHandler(facility=facility)
else:
sysloghandler = logging.handlers.SysLogHandler(address=address, facility=facility)
except Exception as e:
sysloghandler = None
if sysloghandler:
sysloghandler.setLevel(lvl)
sysloghandler.setFormatter(RedactingFormatter(syslogformatter))
log.addHandler(sysloghandler)
log.setLevel(logging.DEBUG)
return log