toderik
toderik

Reputation: 479

Python SocketHandler with plain text

I've set up a SocketHandler but noticed it creates a binary output. I checked the docs and saw that it calls the "makePickle" function to create a binary output from the message record. I use dictConfig() to configure logging.

What I'd like to have is a plain text log message sent out to a TCP server without any pickling. I have two ideas in mind:

  1. Create a custom handler derived from SocketHandler and override makePickle to return the plain text message with the given formatter
  2. Create a custom handler derived from StreamHandler and pass IP and port and initialize stream to be a TCP stream

I can't decide which one is the better solution. Can you guys help me out? Also, if there's any other, easier and more straightforward way to achieve this I'm open to it.

Thanks

Upvotes: 1

Views: 994

Answers (1)

toderik
toderik

Reputation: 479

If anyone has the same problem, I decided to create a custom handler based on Socket handler. Like so:

class PlainTextTcpHandler(handlers.SocketHandler):
    """ Sends plain text log message over TCP channel """


    def makePickle(self, record):
        message = self.formatter.format(record) + "\r\n"
        return message.encode()

And you can use this handler as any other default one.

Upvotes: 3

Related Questions