Skip to content

Commit 7cdcc2e

Browse files
authored
Merge pull request #10 from mikeadamz/master
Add support for including exception information in JSON logs
2 parents 83f6252 + 124bd2b commit 7cdcc2e

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,11 @@ e.g.:
216216
"thread": "MainThread",
217217
"level": "INFO",
218218
"line_no": 22,
219+
"filename": "/path/to/foo.py"
220+
"exc_info": "Traceback (most recent call last): \n File "<stdin>", line 1, in <module>\n ValueError: There is something wrong with your input",
219221
"correlation_id": "1975a02e-e802-11e7-8971-28b2bd90b19a",
220-
"extra_property": "extra_value"
222+
"extra_property": "extra_value",
223+
"msg": "This is a message"
221224
}
222225
```
223226
- Request log: request instrumentation logging statement which recorded request information such as response time, request size, etc.
@@ -268,8 +271,9 @@ Field | Description | Format | Example
268271
msg | The actual message string passed to the logger. | string | This is a log message
269272
level | The log "level" indicating the severity of the log message. | string | INFO
270273
thread | Identifies the execution thread in which this log message has been written. | string | http-nio-4655
271-
logger | The logger name that emits the log message.
272-
| string | requests-logger
274+
logger | The logger name that emits the log message. |string | requests-logger
275+
filename | The file name where an exception originated | string | /path/to/foo.py
276+
exc_info | Traceback information about an exception | string | "Traceback (most recent call last): \n File "<stdin>", line 1, in <module>\n ValueError: There is something wrong with your input"
273277

274278
- request logs:
275279

json_logging/__init__.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import uuid
55
from datetime import datetime
6+
import traceback
67

78
from json_logging import util
89
from json_logging.framework_base import RequestAdapter, ResponseAdapter, AppRequestInstrumentationConfigurator, \
@@ -225,6 +226,21 @@ class JSONLogFormatter(logging.Formatter):
225226
Formatter for non-web application log
226227
"""
227228

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+
228244
def format(self, record):
229245
utcnow = datetime.utcnow()
230246
json_log_object = {"type": "log",
@@ -236,14 +252,17 @@ def format(self, record):
236252
"logger": record.name,
237253
"thread": record.threadName,
238254
"level": record.levelname,
239-
"module": record.module,
240255
"line_no": record.lineno,
241-
"msg": record.getMessage()
256+
"module": record.module,
257+
"msg": record.getMessage(),
242258
}
243259

244260
if hasattr(record, 'props'):
245261
json_log_object.update(record.props)
246262

263+
if record.exc_info or record.exc_text:
264+
json_log_object.update(self.get_exc_fields(record))
265+
247266
return JSON_SERIALIZER(json_log_object)
248267

249268

@@ -252,6 +271,21 @@ class JSONLogWebFormatter(logging.Formatter):
252271
Formatter for web application log
253272
"""
254273

274+
def get_exc_fields(self, record):
275+
if record.exc_info:
276+
exc_info = self.format_exception(record.exc_info)
277+
else:
278+
exc_info = record.exc_text
279+
return {
280+
'exc_info': exc_info,
281+
'filename': record.filename,
282+
}
283+
284+
@classmethod
285+
def format_exception(cls, exc_info):
286+
287+
return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''
288+
255289
def format(self, record):
256290
utcnow = datetime.utcnow()
257291
json_log_object = {"type": "log",
@@ -272,6 +306,9 @@ def format(self, record):
272306
if hasattr(record, 'props'):
273307
json_log_object.update(record.props)
274308

309+
if record.exc_info or record.exc_text:
310+
json_log_object.update(self.get_exc_fields(record))
311+
275312
return JSON_SERIALIZER(json_log_object)
276313

277314

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="json-logging",
15-
version='0.0.9',
15+
version='0.0.10',
1616
packages=find_packages(exclude=['contrib', 'docs', 'tests*', 'example', 'dist', 'build']),
1717
license='Apache License 2.0',
1818
description="JSON Python Logging",

0 commit comments

Comments
 (0)