Correct way logging exception messages in python
Asked Answered
B

1

6

What is most correct way of logging exceptions in Python?

Some people log just e, another logs attribute e.message (but it can be missing for some exceptions).
One use str() to capture description of exception, but it doesn't contain error type and sometimes are useless without that.
Actually repr() will return both error type and description but it is not so popular (in projects that I saw).

One more related question: how this affect services that collect and group errors/alerts like Sentry. They should also contain some information about concrete error.

So want to open this discussion again: which way of logging exceptions is the best to use?

def foo():
    """ Method that can raise various types of exceptions """
    1/0  # just for example

try:
    foo()
except Exception as e:
    logger.exception('Error occurred during execution: %s', e)          # 1
    logger.exception('Error occurred during execution: %s', e.message)  # 2
    logger.exception('Error occurred during execution: %s', str(e))     # 3
    logger.exception('Error occurred during execution: %s', str(e.message))  # 4
    logger.exception('Error occurred during execution: %s', repr(e))    # 5

Found one old discussion here, but it doesn't use logging library and maybe something changed from that moment.

P.S. I know how to get trace-back and any other related information. The question here what should be the general rule for logging exceptions and errors.

Basso answered 8/7, 2019 at 10:3 Comment(11)
Take look there: #5192330Widdershins
Do you mean that there is even no need to add e to message as argument? Maybe I'll need to update this question with also case of use logger.error(...)Basso
@Widdershins also how that will affect services that collect and group issues by these logs (e.g. Sentry).Basso
Yes there is no need to add e, but you can do it, the exceptionn's message will be logged, followed by the traceback of the error. And I don't know for Sentry, I never used it...Widdershins
Possible duplicate of How do I log a Python error with debug information?Instrumental
@Instrumental actually I know how to get traceback and any other information. The question here what should be the general rule for logging exceptions and errors.Basso
If you have a question that can be answered specifically and completely, then this is the place for it. "Most correct" would imply that one's subjective opinion is involved in the answer. You can try reddit's /r/programming or hackernews to host a discussion on this topic.Instrumental
Thanks @Ananth, maybe it is good idea. Will try.Basso
@Basso what informations are relevant wrt/ logging depends on both the exact exception type and the context (your app or lib and how it's used), so there's no generic one-size-fits-all answer. You're using logger.exception() already so the error name and message (and the full traceback) will get logged anyway.Kawasaki
wrt/ "how this affect services that collect and group errors/alerts like Sentry. ", well, it depends on the service itself and (eventually) how it is configured so there's no generic answer here either.Kawasaki
TL;DR : except for 2 and 4 (which are likely to raise attribute errors - exceptions don't necessarily have a "message" attribute), anything else is correct, so use whichever makes sense in the context.Kawasaki
P
2

First, the 2 and 4 solution are not working because an exception don't have any message into it.

Then the 1 and 3 are giving the same result :

division by zero

Those are more user-oriented (but not really working with this example).

Finally, the 5 is displaying :

ZeroDivisionError('division by zero')

It is more developer-oriented, if you want to debug code for example, but in that case, you should allow Python to display error by itself so it will be way more explicit.

Hope I answered to your question !

Piliferous answered 8/7, 2019 at 10:23 Comment(4)
"but not really working with this example" which example do you mean? Generally I know what they produce. Wanted to know some "best practices". Thanks for answer.Basso
Not working with the division by zero because it is a code mistake, not useful for the user to see it.Piliferous
this is just for example. Didn't want to use any real examples or communication with external libraries or anything else.Basso
Yes obviously, but just saying :)Piliferous

© 2022 - 2024 — McMap. All rights reserved.