Python Logging Objects

I am trying to reformat the output sent to the log based on this class.

For instance:

  • Lines
  • will be printed since they
  • dictionaries / lists will be automatically indented / decorated in html
  • my custom classes will be processed on an individual basis and converted to html

My problem is that the message sent to the formatter is always a string. The documentation states that you can send objects in the form of messages, but it looks like they convert the objects to strings before I can format them.

class MyFormatter(logging.Formatter): def format(self, record): #The problem is that record.message is already a string... ... 

Where is the right place to handle objects sent as messages?

+4
source share
2 answers

Ok, I figured it out.

The documentation in the official docs is somewhat unclear, but basically there are two attributes

LogRecord.message → the string representation of the message as well as LogRecord.msg → the message itself.

To get the actual object, you must reference .msg for it to work.

Hope this was helpful to someone else.

+4
source

Perhaps in the __str__() method of the objects you register?

0
source

All Articles