Skip to content

Commit aa77813

Browse files
authored
Fix the exception log issue (#23)
* Fix the exception log issue * Fix the missing fields issue
1 parent 13e9014 commit aa77813

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

cmreslogging/handlers.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
except ImportError:
2222
AWS4AUTH_SUPPORTED = False
2323

24+
from serializers import CMRESSerializer
25+
2426

2527
class CMRESHandler(logging.Handler):
2628
""" Elasticsearch log handler
@@ -196,8 +198,8 @@ def __init__(self,
196198
self._client = None
197199
self._buffer = []
198200
self._timer = None
199-
200201
self._index_name_func = CMRESHandler._INDEX_FREQUENCY_FUNCION_DICT[self.index_name_frequency]
202+
self.serializer = CMRESSerializer()
201203

202204
def __schedule_flush(self):
203205
if self._timer is None:
@@ -211,7 +213,8 @@ def __get_es_client(self):
211213
self._client = Elasticsearch(hosts=self.hosts,
212214
use_ssl=self.use_ssl,
213215
verify_certs=self.verify_certs,
214-
connection_class=RequestsHttpConnection)
216+
connection_class=RequestsHttpConnection,
217+
serializer=self.serializer)
215218
return self._client
216219

217220
if self.auth_type == CMRESHandler.AuthType.BASIC_AUTH:
@@ -220,7 +223,8 @@ def __get_es_client(self):
220223
http_auth=self.auth_details,
221224
use_ssl=self.use_ssl,
222225
verify_certs=self.verify_certs,
223-
connection_class=RequestsHttpConnection)
226+
connection_class=RequestsHttpConnection,
227+
serializer=self.serializer)
224228
return self._client
225229

226230
if self.auth_type == CMRESHandler.AuthType.KERBEROS_AUTH:
@@ -231,7 +235,8 @@ def __get_es_client(self):
231235
use_ssl=self.use_ssl,
232236
verify_certs=self.verify_certs,
233237
connection_class=RequestsHttpConnection,
234-
http_auth=HTTPKerberosAuth(mutual_authentication=DISABLED))
238+
http_auth=HTTPKerberosAuth(mutual_authentication=DISABLED),
239+
serializer=self.serializer)
235240

236241
if self.auth_type == CMRESHandler.AuthType.AWS_SIGNED_AUTH:
237242
if not AWS4AUTH_SUPPORTED:
@@ -243,7 +248,8 @@ def __get_es_client(self):
243248
http_auth=awsauth,
244249
use_ssl=self.use_ssl,
245250
verify_certs=True,
246-
connection_class=RequestsHttpConnection
251+
connection_class=RequestsHttpConnection,
252+
serializer=self.serializer
247253
)
248254
return self._client
249255

@@ -309,11 +315,13 @@ def close(self):
309315
def emit(self, record):
310316
""" Emit overrides the abstract logging.Handler logRecord emit method
311317
312-
records the log
318+
Format and records the log
313319
314320
:param record: A class of type ```logging.LogRecord```
315321
:return: None
316322
"""
323+
self.format(record)
324+
317325
rec = self.es_additional_fields.copy()
318326
for key, value in record.__dict__.items():
319327
if key not in CMRESHandler.__LOGGING_FILTER_FIELDS:

cmreslogging/serializers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
""" JSON serializer for Elasticsearch use
2+
"""
3+
from datetime import date, datetime
4+
from decimal import Decimal
5+
6+
from elasticsearch.serializer import JSONSerializer
7+
8+
class CMRESSerializer(JSONSerializer):
9+
""" JSON serializer inherited from the elastic search JSON serializer
10+
11+
Allows to serialize logs for a elasticsearch use.
12+
Manage the record.exc_info containing an exception type.
13+
"""
14+
def default(self, data):
15+
""" Default overrides the elasticsearch default method
16+
17+
Allows to transform unknown types into strings
18+
19+
:params data: The data to serialize before sending it to elastic search
20+
"""
21+
if isinstance(data, (date, datetime)):
22+
return data.isoformat()
23+
elif isinstance(data, Decimal):
24+
return float(data)
25+
else:
26+
return str(data)

0 commit comments

Comments
 (0)