[python logging] How to define an Exception class that sets ther right `message` and extra info

Hello @hoylemd, we run str(MyError()) on the exception to extract the message. I do not know what making the exception a dataclass does, but I suspect it reverts the default behavior of Exception.__str__. I recommend overriding the __str__ method on your exception classes to return self.message.

As for the extra info: You can pass extra to logger.exception() as per the stdlib documentation. This should allow you to write:

class MyError(...):
    ...
    def __str__(self): return self.message

...
logging.exception(myerror, extra=myerror.__dict__)

I’m making the assumption that __dict__ actually exists on dataclasses but you get the idea.

Let me know if that works for you.