-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathlogger.py
executable file
·226 lines (195 loc) · 6.88 KB
/
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
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
'''
__G__ = "(G)bd249ce4"
logger -> main
'''
from os import path, environ
from contextlib import contextmanager
from sys import stdout
from datetime import datetime
from tempfile import gettempdir
from logging import DEBUG, Handler, WARNING, getLogger
from gevent import Timeout
from analyzer.settings import json_settings, defaultdb
from analyzer.connections.mongodbconn import add_item_fs, add_item, update_task
LOGTERMINAL, DYNAMIC, VERBOSE_FLAG, VERBOSE_TIMEOUT = None, None, None, None
ENV_VAR = environ["analyzer_env"]
if DYNAMIC is None:
DYNAMIC = getLogger("analyzerDYNAMIC")
if LOGTERMINAL is None:
LOGTERMINAL = getLogger("analyzerLOGTERMINAL")
if VERBOSE_FLAG is None:
VERBOSE_FLAG = False
if VERBOSE_TIMEOUT is None:
VERBOSE_TIMEOUT = json_settings[ENV_VAR]["function_timeout"]
class TerminalColors:
'''
Colors (add more)
'''
Restore = '\033[0m'
Black = "\033[030m"
Red = "\033[91m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
GREEN_X = '{}{}{}'.format(TerminalColors.Green, "X", TerminalColors.Restore)
YELLOW_ARROW = '{}{}{}'.format(TerminalColors.Yellow, ">", TerminalColors.Restore)
EXCLAMATION_MARK = '{}{}{}'.format(TerminalColors.Yellow, "!", TerminalColors.Restore)
RED_ARROW = '{}{}{}'.format(TerminalColors.Red, ">", TerminalColors.Restore)
@contextmanager
def ignore_excpetion(*exceptions):
'''
catch excpetion
'''
try:
yield
except exceptions as error:
#print("{} {} {}".format(datetime.utcnow(), EXCLAMATION_MARK, error))
pass
class Unbuffered:
'''
unused
'''
def __init__(self, stream):
'''
unused
'''
self.stream = stream
def write(self, data):
'''
write stream and flush (unbuffered)
'''
self.stream.write(data)
self.stream.flush()
# te.write(data)
class CustomHandler(Handler):
'''
custom log handler for adding logs to file as well
'''
def __init__(self, file):
'''
initialize and prepare the file
'''
Handler.__init__(self)
self.logsfile = open(file, 'w')
def emit(self, record):
'''
override emit
'''
print("{} {} {}".format(record.msg[0], record.msg[2], record.msg[1]))
stdout.flush()
self.logsfile.write("{} {} {}\n".format(record.msg[0], record.msg[2], record.msg[1]))
self.logsfile.flush()
add_item(defaultdb["dbname"], defaultdb["alllogscoll"], {'time': record.msg[0], 'message': record.msg[1]})
class TaskHandler(Handler):
'''
task log handler for adding logs to file as well
'''
def __init__(self, task):
'''
initialize and prepare the file
'''
Handler.__init__(self)
self.logsfile = open(path.join(json_settings[ENV_VAR]["logs_folder"], task), 'w')
self.task = task
def emit(self, record):
'''
override emit
'''
self.logsfile.write("{} {}\n".format(record.msg[0], record.msg[1]))
self.logsfile.flush()
update_task(defaultdb["dbname"], defaultdb["taskdblogscoll"], self.task, "{} {}".format(record.msg[0], record.msg[1]))
def setup_task_logger(task):
'''
setup the dynamic logger for the task
'''
log_string("Setting up task {} logger".format(task), "Yellow")
add_item(defaultdb["dbname"], defaultdb["taskdblogscoll"], {"task": task, "logs": []})
DYNAMIC.handlers.clear()
DYNAMIC.setLevel(DEBUG)
DYNAMIC.addHandler(TaskHandler(task))
DYNAMIC.disabled = False
def cancel_task_logger(task):
'''
cancel dynamic logger for the task
'''
log_string("Closing up task {} logger".format(task), "Yellow")
DYNAMIC.disabled = True
logs = ""
with open(path.join(json_settings[ENV_VAR]["logs_folder"], task), "rb") as file:
logs = file.read()
if len(logs) > 0:
_id = add_item_fs(defaultdb["dbname"], defaultdb["taskfileslogscoll"], logs, "log", None, task, "text/plain", datetime.now())
if _id:
log_string("Logs result dumped into db", "Yellow")
else:
log_string("Unable to dump logs result to db", "Red")
else:
log_string("Unable to dump logs result to db", "Red")
def log_string(_str, color, on_off=True):
'''
output str with color and symbol (they are all as info)
'''
ctime = datetime.utcnow()
if color == "Green":
LOGTERMINAL.info([ctime, _str, GREEN_X])
DYNAMIC.info([ctime, _str, "X"])
elif color == "Yellow":
LOGTERMINAL.info([ctime, _str, YELLOW_ARROW])
DYNAMIC.info([ctime, _str, ">"])
elif color == "Red":
LOGTERMINAL.info([ctime, _str, RED_ARROW])
DYNAMIC.info([ctime, _str, ">"])
elif color == "Yellow!":
LOGTERMINAL.info([ctime, _str, EXCLAMATION_MARK])
DYNAMIC.info([ctime, _str, "!"])
def verbose(on_off=False, verbose_output=False, timeout=None, _str=None):
'''
decorator functions for debugging (show basic args, kwargs)
'''
def decorator(func):
def wrapper(*args, **kwargs):
result = None
#global pool
function_name = func.__module__ + "." + func.__name__
try:
if not on_off:
log_string(function_name, "Yellow")
if verbose_output:
log_string("Function '{0}', parameters :{1} and {2}".format(func.__name__, args, kwargs), "Yellow")
if _str:
log_string(_str, "Green")
if _str == "Starting Analyzer":
if timeout is None:
time = json_settings[ENV_VAR]["analyzer_timeout"]
else:
time = timeout
else:
if timeout is None:
time = json_settings[ENV_VAR]["function_timeout"]
else:
time = timeout
try:
with Timeout(time):
result = func(*args, **kwargs)
except Timeout:
log_string("{} > {}s.. Timeout".format(function_name, time), "Red")
except Exception as error:
log_string("{}.{} Failed -> {}".format(func.__module__, func.__name__, error), "Red")
except Exception as error:
log_string("{}.{} Failed -> {}".format(func.__module__, func.__name__, error), "Red")
return result
return wrapper
return decorator
def setup_logger():
'''
main logger logic
'''
getLogger("requests").setLevel(WARNING)
getLogger("urllib3").setLevel(WARNING)
getLogger("PIL").setLevel(WARNING)
getLogger("chardet").setLevel(WARNING)
LOGTERMINAL.setLevel(DEBUG)
LOGTERMINAL.addHandler(CustomHandler(path.join(gettempdir(), "alllogs")))