-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiemplifyLogger.py
278 lines (237 loc) · 9.73 KB
/
SiemplifyLogger.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import logging.handlers
import logging.config
import json
from os import path
import traceback
from abc import ABCMeta, abstractmethod
from sys import stderr
import six
import arrow
import SiemplifyUtils
from SiemplifyDataModel import ConnectorLogRecord, LogRecordTypeEnum, ActionLogRecord, LogRow
from SiemplifyUtils import is_python_37
class LogLevelEnum(object):
INFO = 1
WARN = 2
ERROR = 3
class SiemplifyLogger(object):
DEFAULT_LOGGER_NAME = "siemplify_default_logger"
DEFAULT_FILE_HANDLER_NAME = "siempify_file_handler"
DEFAULT_LOG_FILE_NAME = "logdata.log"
DEFAULT_LOG_LOCATION = "SDK"
def __init__(self, log_path, log_location=DEFAULT_LOG_LOCATION, module=None, logs_collector=None):
self.config_file_path = path.join(path.dirname(__file__), 'ScriptingLogging.config')
self._error_logged = False
self._log = None
self._logs_collector = logs_collector
self._log_rows = []
try:
config = self.loadConfigFromFile(log_path, log_location)
logging.config.dictConfig(config)
self._log = logging.getLogger(self.DEFAULT_LOGGER_NAME)
self.module = module
except:
SiemplifyLogger.print_to_stderr("LOGGER: Error initializing")
traceback.print_exc()
def loadConfigFromFile(self, log_path, log_location):
"""
load config file
:param run_folder: {string} running folder path
:param log_location: {string} elastic search log location
:return:
"""
try:
configfile = open(self.config_file_path, "r")
config_json_string = configfile.read()
configfile.close()
logging_config_from_file = json.loads(config_json_string)
handlers = logging_config_from_file["handlers"]
if self.DEFAULT_FILE_HANDLER_NAME in handlers and log_path:
handlers[self.DEFAULT_FILE_HANDLER_NAME]["filename"] = log_path
return logging_config_from_file
except:
SiemplifyLogger.print_to_stderr("LOGGER: loadConfigFromFile FAILED")
def exception(self, message, *args, **kwargs):
"""
configure log - type exception
:param message: {string} exception message
"""
self._error_logged = True
try:
self.append_message(message, LogLevelEnum.ERROR)
self.safe_print(message)
if self._log:
if isinstance(message, Exception) and not is_python_37():
escaped_msg = str(message.message).replace("%", "%%")
elif is_python_37():
escaped_msg = str(message).replace("%", "%%")
else:
escaped_msg = message.replace("%", "%%")
if self.module:
kwargs.update({'module': self.module})
if kwargs:
self._log.error(escaped_msg, kwargs, exc_info=True)
else:
self._log.error(escaped_msg, exc_info=True)
if self._logs_collector:
self._logs_collector.collect(message, LogRecordTypeEnum.ERROR)
except Exception as e:
SiemplifyLogger.print_to_stderr("LOGGER.exception FAILED.")
def error(self, message, *args, **kwargs):
"""
configure log - type error
:param message: {string} exception message
"""
self._error_logged = True
try:
self.append_message(message, LogLevelEnum.ERROR)
self.safe_print(message)
if self._log:
if self.module:
kwargs.update({'module': self.module})
if kwargs:
self._log.error(message.replace("%", "%%"), kwargs)
else:
self._log.error(message)
if self._logs_collector:
self._logs_collector.collect(message, LogRecordTypeEnum.ERROR)
except:
SiemplifyLogger.print_to_stderr("LOGGER.error FAILED")
def warn(self, message, *args, **kwargs):
"""
configure log - type warn
:param message: {string} exception message
"""
try:
self.append_message(message, LogLevelEnum.WARN)
self.safe_print(message)
if self._log:
if self.module:
kwargs.update({'module': self.module})
if kwargs:
self._log.warn(message.replace("%", "%%"), kwargs)
else:
self._log.warn(message)
if self._logs_collector:
self._logs_collector.collect(message, LogRecordTypeEnum.ERROR)
except:
SiemplifyLogger.print_to_stderr("LOGGER.warn FAILED")
def info(self, message, *args, **kwargs):
"""
configure log - type info
:param message: {string} exception message
"""
try:
self.append_message(message, LogLevelEnum.INFO)
self.safe_print(message)
if self._log:
if self.module:
kwargs.update({'module': self.module})
if kwargs:
self._log.info(message.replace("%", "%%"), kwargs)
else:
self._log.info(message)
if self._logs_collector:
self._logs_collector.collect(message, LogRecordTypeEnum.INFO)
except Exception as e:
SiemplifyLogger.print_to_stderr(u"LOGGER.info FAILED")
def append_message(self, message, log_level):
try:
log_row = LogRow(message=SiemplifyLogger.encode(str(message)),
log_level=log_level,
timestamp=SiemplifyUtils.unix_now())
self._log_rows.append(log_row)
except Exception as e:
print("Couldn't append log message, reason: {exception}".format(e))
@staticmethod
def encode(message):
try:
if not is_python_37() and isinstance(message, unicode):
return message.encode("utf8")
else:
return message
except Exception as e:
print("Couldn't encode message, reason: {exception}".format(e))
@staticmethod
def safe_print(message):
try:
print(SiemplifyLogger.encode(message))
except Exception as e:
print("Couldn't print exception, check log")
@property
def log_rows(self):
return self._log_rows
@staticmethod
def print_to_stderr(message):
stderr.write("LOGGER: loadConfigFromFile FAILED")
@property
def error_logged(self):
return self._error_logged
class SiempplifyConnectorsLogger(object):
_log_items = []
def error(self, message):
msg = "ERROR | " + str(message)
print(msg)
self._log_items.append(msg)
def warn(self, message):
msg = "WARN | " + str(message)
print(msg)
self._log_items.append(msg)
def info(self, message):
msg = "INFO | " + str(message)
print(msg)
self._log_items.append(msg)
@six.add_metaclass(ABCMeta)
class FileLogsCollector(object):
LOG_COLLECTOR_FILENAME = "logs_collector.json"
def __init__(self, file_dir):
self.log_collector_file_path = path.join(file_dir, self.LOG_COLLECTOR_FILENAME)
@abstractmethod
def create_log_record(self, message, log_type):
raise NotImplementedError
def collect(self, message, log_type):
log_record = self.create_log_record(message, log_type)
log_items = []
try:
if path.exists(self.log_collector_file_path):
log_items = json.load(open(self.log_collector_file_path, "r"))
with open(self.log_collector_file_path, 'w') as logs_collector:
log_items.append(log_record)
logs_collector.write(json.dumps(log_items, default=lambda o: o.__dict__))
except Exception:
stderr.write("LOGGER: add log record to collector FAILED")
class ConnectorsFileLogsCollector(FileLogsCollector):
"""Collect connectors logs to a file"""
def __init__(self, file_dir, connector_context):
super(ConnectorsFileLogsCollector, self).__init__(file_dir)
self.connector_context = connector_context
def create_log_record(self, message, log_type):
log_record = ConnectorLogRecord(
record_type=log_type,
message=message,
connector_identifier=self.connector_context.connector_info.identifier,
result_data_type=self.connector_context.connector_info.result_data_type,
source_system_name=self.connector_context.connector_info.integration,
connector_definition_name=self.connector_context.connector_info.connector_definition_name,
integration=self.connector_context.connector_info.integration,
timestamp=arrow.utcnow().timestamp
)
return log_record
class ActionsFileLogsCollector(FileLogsCollector):
"""Collect actions logs to a file"""
def __init__(self, file_dir, context_data):
super(ActionsFileLogsCollector, self).__init__(file_dir)
self.context_data = context_data
def create_log_record(self, message, log_type):
log_record = ActionLogRecord(
record_type=log_type,
message=message,
case_id=self.context_data['case_id'],
alert_id=self.context_data['alert_id'],
workflow_id=self.context_data['workflow_id'],
environment=self.context_data["environment"],
action_definition_name=self.context_data["action_definition_name"],
integration=self.context_data["integration_identifier"],
timestamp=arrow.utcnow().timestamp
)
return log_record