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.

11
  • Take look there: stackoverflow.com/questions/5191830/… Commented Jul 8, 2019 at 10:08
  • 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(...) Commented Jul 8, 2019 at 10:11
  • @olinox14 also how that will affect services that collect and group issues by these logs (e.g. Sentry). Commented Jul 8, 2019 at 10:17
  • 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... Commented Jul 8, 2019 at 10:19
  • Possible duplicate of How do I log a Python error with debug information? Commented Jul 8, 2019 at 10:51

1 Answer 1

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 !

Sign up to request clarification or add additional context in comments.

4 Comments

"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.
Not working with the division by zero because it is a code mistake, not useful for the user to see it.
this is just for example. Didn't want to use any real examples or communication with external libraries or anything else.
Yes obviously, but just saying :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.