Skip to content

Commit

Permalink
Merge pull request #10 from mikeadamz/master
Browse files Browse the repository at this point in the history
Add support for including exception information in JSON logs
  • Loading branch information
bobbui authored Dec 4, 2018
2 parents 83f6252 + 124bd2b commit 7cdcc2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ e.g.:
"thread": "MainThread",
"level": "INFO",
"line_no": 22,
"filename": "/path/to/foo.py"
"exc_info": "Traceback (most recent call last): \n File "<stdin>", line 1, in <module>\n ValueError: There is something wrong with your input",
"correlation_id": "1975a02e-e802-11e7-8971-28b2bd90b19a",
"extra_property": "extra_value"
"extra_property": "extra_value",
"msg": "This is a message"
}
```
- Request log: request instrumentation logging statement which recorded request information such as response time, request size, etc.
Expand Down Expand Up @@ -268,8 +271,9 @@ Field | Description | Format | Example
msg | The actual message string passed to the logger. | string | This is a log message
level | The log "level" indicating the severity of the log message. | string | INFO
thread | Identifies the execution thread in which this log message has been written. | string | http-nio-4655
logger | The logger name that emits the log message.
| string | requests-logger
logger | The logger name that emits the log message. |string | requests-logger
filename | The file name where an exception originated | string | /path/to/foo.py
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"

- request logs:

Expand Down
41 changes: 39 additions & 2 deletions json_logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import uuid
from datetime import datetime
import traceback

from json_logging import util
from json_logging.framework_base import RequestAdapter, ResponseAdapter, AppRequestInstrumentationConfigurator, \
Expand Down Expand Up @@ -225,6 +226,21 @@ class JSONLogFormatter(logging.Formatter):
Formatter for non-web application log
"""

def get_exc_fields(self, record):
if record.exc_info:
exc_info = self.format_exception(record.exc_info)
else:
exc_info = record.exc_text
return {
'exc_info': exc_info,
'filename': record.filename,
}

@classmethod
def format_exception(cls, exc_info):

return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''

def format(self, record):
utcnow = datetime.utcnow()
json_log_object = {"type": "log",
Expand All @@ -236,14 +252,17 @@ def format(self, record):
"logger": record.name,
"thread": record.threadName,
"level": record.levelname,
"module": record.module,
"line_no": record.lineno,
"msg": record.getMessage()
"module": record.module,
"msg": record.getMessage(),
}

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

if record.exc_info or record.exc_text:
json_log_object.update(self.get_exc_fields(record))

return JSON_SERIALIZER(json_log_object)


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

def get_exc_fields(self, record):
if record.exc_info:
exc_info = self.format_exception(record.exc_info)
else:
exc_info = record.exc_text
return {
'exc_info': exc_info,
'filename': record.filename,
}

@classmethod
def format_exception(cls, exc_info):

return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''

def format(self, record):
utcnow = datetime.utcnow()
json_log_object = {"type": "log",
Expand All @@ -272,6 +306,9 @@ def format(self, record):
if hasattr(record, 'props'):
json_log_object.update(record.props)

if record.exc_info or record.exc_text:
json_log_object.update(self.get_exc_fields(record))

return JSON_SERIALIZER(json_log_object)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="json-logging",
version='0.0.9',
version='0.0.10',
packages=find_packages(exclude=['contrib', 'docs', 'tests*', 'example', 'dist', 'build']),
license='Apache License 2.0',
description="JSON Python Logging",
Expand Down

0 comments on commit 7cdcc2e

Please sign in to comment.