The logging module has a class called a QueueHandler
which accepts log record events and puts them onto a queue. The records are then typically read from the queue by a QueueListener
object. I’m trying to generate functionality for logging dictionaries instead of strings so I might make calls like logger.INFO({'dict_message': 'Hello World!', metadata: 42})
. In this case I want the message which is a dictionary to get passed through to the endpoint handlers without being modified. Unfortunately, however, the QueueListener
puts the record through a prepare()
method which essentially does record.msg = self.format(record.msg)
using the default formatter for the QueueHandler
object. The default formatter ends up turning my dict into a string.
The documentation for the method indicates that the purpose of the prepare()
method is to ensure the queued record is pickleable. The documentation also specifically indicates that you may want to override this method if you are converting the record into a dict (which is what I’m doing) so I’ve found an approach that works for my use case.
But my question is essentially: Why does the QueueHandler
need any prepare method at all? Why not just queue whatever record is passed in directly? Why should the QueueHandler
have anything so say about whether a record is pickleable or not? That seems out of scope for its job. Perhaps I’m missing something about queueing in python and not all types of objects can be put in a queue? So the prepare
method is doing some safety to make sure the queueing will go ok? If this is not the reason then what is the reason for the prepare()
method on QueueHandler
?