Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Serialization Improvements for GELFRabbitHandler #91

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions graypy/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,19 @@ def smarter_repr(obj):
"""Convert JSON incompatible object to string"""
if isinstance(obj, datetime.datetime):
return obj.isoformat()

return repr(obj)

def introspective_repr(obj):
""" introspective repr() - alternative to smarter_repr() """

if isinstance(obj, datetime.datetime):
return obj.isoformat()

if isinstance(obj, enum.Enum):
return repr({obj.name: obj.value})

return repr(dir(obj))

def message_to_pickle(obj):
"""Convert object to a JSON-encoded string"""
Expand Down
7 changes: 6 additions & 1 deletion graypy/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
handler"""

import json
from graypy.handler import make_message_dict, introspective_repr
from logging import Filter
from logging.handlers import SocketHandler

Expand Down Expand Up @@ -85,7 +86,11 @@ def makePickle(self, record):
self.localname,
self.facility
)
return json.dumps(message_dict)

try:
return json.dumps(message_dict)
except TypeError:
return json.dumps(message_dict, default=introspective_repr)


class RabbitSocket(object):
Expand Down