5

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.

wowkin2
  • 5,895
  • 5
  • 23
  • 66
  • Take look there: https://stackoverflow.com/questions/5191830/how-do-i-log-a-python-error-with-debug-information – olinox14 Jul 08 '19 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(...)` – wowkin2 Jul 08 '19 at 10:11
  • @olinox14 also how that will affect services that collect and group issues by these logs (e.g. Sentry). – wowkin2 Jul 08 '19 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... – olinox14 Jul 08 '19 at 10:19
  • Possible duplicate of [How do I log a Python error with debug information?](https://stackoverflow.com/questions/5191830/how-do-i-log-a-python-error-with-debug-information) – Ananth Jul 08 '19 at 10:51
  • @Ananth 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. – wowkin2 Jul 08 '19 at 11:08
  • 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. – Ananth Jul 08 '19 at 11:17
  • Thanks @Ananth, maybe it is good idea. Will try. – wowkin2 Jul 08 '19 at 11:29
  • @wowkin2 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. – bruno desthuilliers Jul 08 '19 at 11:42
  • 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. – bruno desthuilliers Jul 08 '19 at 11:43
  • 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. – bruno desthuilliers Jul 08 '19 at 11:46

1 Answers1

1

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 !

B.Medica
  • 11
  • 2
  • "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. – wowkin2 Jul 08 '19 at 10:28
  • Not working with the division by zero because it is a code mistake, not useful for the user to see it. – B.Medica Jul 08 '19 at 10:30
  • 1
    this is just for example. Didn't want to use any real examples or communication with external libraries or anything else. – wowkin2 Jul 08 '19 at 11:06
  • Yes obviously, but just saying :) – B.Medica Jul 08 '19 at 12:07