-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoohLog.py
209 lines (177 loc) · 5.78 KB
/
MoohLog.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
import codecs
import datetime
import re
import sys
import json
import traceback
class MoohLogger(object):
DEBUG = 0
INFO = 10
WARNING = 20
ERROR = 30
EXCEPTION = 40
FATAL = 50
LEVELS = { DEBUG:"DEBUG", INFO:"INFO", WARNING:"WARNING", ERROR:"ERROR", EXCEPTION:"EXCEPTION", FATAL:"FATAL" }
def __init__(self):
self.writers = []
def log(self,level,data):
data.level = level
for writer in self.writers:
writer.write(data)
def debug(self,x):
self.log(MoohLogger.DEBUG,x)
def info(self,x):
self.log(MoohLogger.INFO,x)
def warning(self,x):
self.log(MoohLogger.WARNING,x)
def error(self,x):
self.log(MoohLogger.ERROR,x)
def fatal(self,x):
self.log(MoohLogger.FATAL,x)
def exception(self):
exc_type, exc_value, exc_traceback = sys.exc_info()
self.error(eventmessage("exception","".join(traceback.format_exception(exc_type, exc_value,exc_traceback))))
class logwriter(object):
def __init__(self):
self.filters = [{}]
def write(self,message):
if message.meets_filter(self.filters):
self.inner_write(message)
def inner_write(self,message):
raise NotImplementedError
class filewriter(logwriter):
def __init__(self,fileformat):
super(filewriter,self).__init__()
self.fileformat = fileformat
def inner_write(self,message):
try:
with codecs.open(datetime.datetime.now().strftime(self.fileformat), "a", "utf-8") as myfile:
myfile.write(str(message)+"\r\n")
except Exception:
pass
class consolewriter(logwriter):
def __init__(self):
super(consolewriter,self).__init__()
def inner_write(self,message):
try:
print(str(message))
except Exception:
pass
MOOHLOGFILTERS = {
"ge": lambda a,b: float(a)>=float(b),
"gt": lambda a,b: float(a)>float(b),
"le": lambda a,b: float(a)<=float(b),
"lt": lambda a,b: float(a)<float(b),
"eq": lambda a,b: float(a)==float(b),
"ne": lambda a,b: float(a)!=float(b),
"isnull": lambda a,b: (a==None) == b,
"exact": lambda a,b: a==b,
"iexact": lambda a,b: a.lower()==b.lower(),
"containedby": lambda a,b: a in b,
"icontainedby": lambda a,b: a.lower() in b.lower(),
"contains": lambda a,b: b in a,
"icontains": lambda a,b: b.lower() in a.lower(),
"startswith": lambda a,b: a.startswith(b),
"endswith": lambda a,b: a.endswith(b),
}
def filter_value(key,val,data):
if len(key) == 0:
return True # if we have an empty key, we just skip it.
# keys starting with ! invert the filter result
invert = key[0] == "!"
if invert:
key = key[1:]
try:
key_name, query = key.rsplit("__",1)
except Exception:
key_name = key
query = "exact"
keys = key_name.split(".")
try:
for k in keys:
data = data[k]
except Exception:
return invert
try:
return MOOHLOGFILTERS[query](data,val) ^ invert
except Exception:
pass
try:
return (val == data) ^ invert
except Exception:
return invert
def filter_dict(data,f):
# list items are ORed
for conj in f:
# dict items are ANDed
allok = True
for key, val in conj.items():
if not filter_value(key, val, data):
allok = False
if allok:
return True
return False
class logmessage(object):
def __init__(self,level=0):
self.level = level
self.type = "generic"
def meets_filter(self,filters):
return filter_dict(self.serialize(), filters)
def inner_str(self):
raise NotImplementedError
def __str__(self):
levelname = ""
maxl = -10
for l,n in MoohLogger.LEVELS.items():
if l<=self.level:
if l>maxl:
levelname = "[%s] "%(n,)
maxl = l
return levelname+self.inner_str()
def serialize(self):
return self.__dict__
class chatmessage(logmessage):
def __init__(self,server,ip,channel,channelid,nick,message):
self.server = server
self.ip = ip
self.channel = channel
self.id = channelid
self.nick = nick
self.message = message
self.type = "chat"
def inner_str(self):
return "[{server}] #{channel} <{nick}>: {message}".format(
server = self.server,
channel = self.channel,
nick = self.nickname,
message = self.message
)
def reploauth(m):
return "oauth:(%d)"%(len(m.group(1)),)
def reploauth_token(m):
return "oauth_token=(%d)"%(len(m.group(1)),)
class eventmessage(logmessage):
def __init__(self,event,message):
self.event = event
self.message = re.sub("oauth:([a-z0-9]+)",reploauth,message)
self.message = re.sub("oauth_token=([a-z0-9]+)",reploauth_token,self.message)
self.type = "event"
self.time = datetime.datetime.now()
def inner_str(self):
return "[%s] [%s] %s"%(self.time.strftime("%H:%M:%S"), self.event, self.message)
class statusmessage(logmessage):
def __init__(self,stats,type):
self.data = stats
self.type = type
def inner_str(self):
return json.dumps(self.data)
if __name__ == "__main__":
l = MoohLogger()
c = consolewriter()
c.filters = [{"event":"test","level__ge":10}]
#c.filters.append({"bro_lt":"1337"})
l.writers.append(c)
l.debug(eventmessage("test","testing events"))
l.error(eventmessage("test","testing events"))
l.fatal(eventmessage("test","testing events"))
l.info(statusmessage({"dank":"memes","bro":420},"status"))