3
3
import logging
4
4
import uuid
5
5
from datetime import datetime
6
+ import traceback
6
7
7
8
from json_logging import util
8
9
from json_logging .framework_base import RequestAdapter , ResponseAdapter , AppRequestInstrumentationConfigurator , \
@@ -225,6 +226,21 @@ class JSONLogFormatter(logging.Formatter):
225
226
Formatter for non-web application log
226
227
"""
227
228
229
+ def get_exc_fields (self , record ):
230
+ if record .exc_info :
231
+ exc_info = self .format_exception (record .exc_info )
232
+ else :
233
+ exc_info = record .exc_text
234
+ return {
235
+ 'exc_info' : exc_info ,
236
+ 'filename' : record .filename ,
237
+ }
238
+
239
+ @classmethod
240
+ def format_exception (cls , exc_info ):
241
+
242
+ return '' .join (traceback .format_exception (* exc_info )) if exc_info else ''
243
+
228
244
def format (self , record ):
229
245
utcnow = datetime .utcnow ()
230
246
json_log_object = {"type" : "log" ,
@@ -237,13 +253,15 @@ def format(self, record):
237
253
"thread" : record .threadName ,
238
254
"level" : record .levelname ,
239
255
"module" : record .module ,
240
- "line_no" : record .lineno ,
241
- "msg" : record .getMessage ()
256
+ "msg" : record .getMessage (),
242
257
}
243
258
244
259
if hasattr (record , 'props' ):
245
260
json_log_object .update (record .props )
246
261
262
+ if record .exc_info or record .exc_text :
263
+ json_log_object .update (self .get_exc_fields (record ))
264
+
247
265
return JSON_SERIALIZER (json_log_object )
248
266
249
267
@@ -252,6 +270,21 @@ class JSONLogWebFormatter(logging.Formatter):
252
270
Formatter for web application log
253
271
"""
254
272
273
+ def get_exc_fields (self , record ):
274
+ if record .exc_info :
275
+ exc_info = self .format_exception (record .exc_info )
276
+ else :
277
+ exc_info = record .exc_text
278
+ return {
279
+ 'exc_info' : exc_info ,
280
+ 'filename' : record .filename ,
281
+ }
282
+
283
+ @classmethod
284
+ def format_exception (cls , exc_info ):
285
+
286
+ return '' .join (traceback .format_exception (* exc_info )) if exc_info else ''
287
+
255
288
def format (self , record ):
256
289
utcnow = datetime .utcnow ()
257
290
json_log_object = {"type" : "log" ,
@@ -272,6 +305,9 @@ def format(self, record):
272
305
if hasattr (record , 'props' ):
273
306
json_log_object .update (record .props )
274
307
308
+ if hasattr (record , 'exc_info' ) or hasattr (record , 'exc_text' ):
309
+ json_log_object .update (self .get_exc_fields (record ))
310
+
275
311
return JSON_SERIALIZER (json_log_object )
276
312
277
313
0 commit comments