From 8d659a269601774aa689ace4e23d06182b482a81 Mon Sep 17 00:00:00 2001 From: "chris.platts" Date: Wed, 10 Oct 2018 16:10:00 +0100 Subject: [PATCH 1/2] - Made smarter_repr smarter (handles objects that are part of nameko's worker_ctx) - Told GELFRabbitHandler to use smarter_repr when serialising its final object --- graypy/handler.py | 5 +++++ graypy/rabbitmq.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/graypy/handler.py b/graypy/handler.py index f075952db..77b0cc469 100644 --- a/graypy/handler.py +++ b/graypy/handler.py @@ -9,6 +9,7 @@ import socket import ssl import math +import enum from logging.handlers import DatagramHandler, SocketHandler PY3 = sys.version_info[0] == 3 @@ -276,6 +277,10 @@ def smarter_repr(obj): """ convert JSON incompatible object to string""" if isinstance(obj, datetime.datetime): return obj.isoformat() + if isinstance(obj, enum.Enum): + return repr({obj.name: obj.value}) + if hasattr(obj, "__dict__"): + return repr(obj.__dict__) return repr(obj) diff --git a/graypy/rabbitmq.py b/graypy/rabbitmq.py index d519067f9..2c1481bc2 100644 --- a/graypy/rabbitmq.py +++ b/graypy/rabbitmq.py @@ -1,6 +1,6 @@ import json from amqplib import client_0_8 as amqp -from graypy.handler import make_message_dict +from graypy.handler import make_message_dict, smarter_repr from logging import Filter from logging.handlers import SocketHandler @@ -71,7 +71,7 @@ def makePickle(self, record): message_dict = make_message_dict( record, self.debugging_fields, self.extra_fields, self.fqdn, self.localname, self.facility) - return json.dumps(message_dict) + return json.dumps(message_dict, default=smarter_repr) class RabbitSocket(object): From b69b725379d23ac45f3941d4c78104c244082934 Mon Sep 17 00:00:00 2001 From: "chris.platts" Date: Mon, 29 Oct 2018 10:53:26 +0000 Subject: [PATCH 2/2] - Returns smarter_repr to its original behaviour - Add introspective_repr function which uses repr(dir(obj)) - Catch TypeError in RabbitHandler's makePickle, and retry with introspective_repr --- graypy/handler.py | 15 ++++++++++++--- graypy/rabbitmq.py | 8 ++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/graypy/handler.py b/graypy/handler.py index 77b0cc469..595a232dd 100644 --- a/graypy/handler.py +++ b/graypy/handler.py @@ -277,11 +277,20 @@ 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}) - if hasattr(obj, "__dict__"): - return repr(obj.__dict__) - return repr(obj) + + return repr(dir(obj)) + def message_to_pickle(obj): diff --git a/graypy/rabbitmq.py b/graypy/rabbitmq.py index 2c1481bc2..3f6a3755e 100644 --- a/graypy/rabbitmq.py +++ b/graypy/rabbitmq.py @@ -1,6 +1,6 @@ import json from amqplib import client_0_8 as amqp -from graypy.handler import make_message_dict, smarter_repr +from graypy.handler import make_message_dict, introspective_repr from logging import Filter from logging.handlers import SocketHandler @@ -71,7 +71,11 @@ def makePickle(self, record): message_dict = make_message_dict( record, self.debugging_fields, self.extra_fields, self.fqdn, self.localname, self.facility) - return json.dumps(message_dict, default=smarter_repr) + + try: + return json.dumps(message_dict) + except TypeError: + return json.dumps(message_dict, default=introspective_repr) class RabbitSocket(object):