I have a custom object that I want to log using python's logger module.
I log it with a dict:
logger.info({
'message': 'something happened',
'object': my_custom_object,
})
The problem is logger uses ujson.dumps()
to serialize the inputs to logger.info()
.
ujson.dumps()
converts my_custom_object to a list of the names of instance variables. This isn't very helpful. I want to specify a custom toJson
or toDict
method to use when trying to serialize this method instead.
Is this possible?
A bit like how you can specify a __str__
method and any time python tries to convert your object to string it will call that method instead.
Apparently you actually can implement a toDict
method on the object and it will work.