News for hackers. Stuff that matters.
Re:Flake8-Logging by teddyh on Monday September 11, 2023 @05:35PM (#37474039)
> LOG005 use exception() within an exception handler

In Python, exception handling is part of the normal control flow. Therefore, when an exception happens, it is not always the case that you want to log a full stack trace.

  try:
    thing = things[0]
  except IndexError:
    log.error("Not enough things!")
    return
I think that even recommending a log.exception() here would actually be a bad thing.
Re:Flake8-Logging by kamikaz1k on Monday September 11, 2023 @08:24PM (#37475471)
If it’s expected control flow, would you log it?

If it’s expected, I tend not to log it.

Re:Flake8-Logging by msm_ on Monday September 11, 2023 @08:42PM (#37475624)
It depends on the situation. There are cases when I "expect the exception" - for example anything that touches the network has a tiny chance of failing. I want to know how often this happen, because too many network errors probably means that something is down and needs to be investigated (and we have alerts for this, based on logs). But to be fair I usually just log .exception() in this case anyway (even if I know exactly what the exception will be).
Re:Flake8-Logging by teddyh on Tuesday September 12, 2023 @02:43AM (#37477587)
Oh no, I would not necessarily log an exception, the same way I don’t log every time an ”else” clause is reached. But in the places I do want to log.error("something") in an exception handler, I don’t want some linter complaining that since there is an active exception, it thinks I ought to have used log.exception().
Re:Flake8-Logging by globular-toast on Tuesday September 12, 2023 @05:07AM (#37478470)
Depends what you're logging. An info level log is quite explicitly for an expected path, e.g. "config not found, using defaults".

This rule is only for error calls, though. In that case, yeah, you wouldn't log an error for an expected path.

Re:Flake8-Logging by bobbyi on Monday September 11, 2023 @08:51PM (#37475680)
log.exception is also usually bad when you are re-raising the exception. That often results in same stacktrace logged multiple times in a row which can be confusing or at least annoyingly verbose.

But it can be helpful to log an error message (without stacktrace) to give context that is available only in this frame before letting the exception propagate to whoever ultimately handles and logs it.

Re:Flake8-Logging by globular-toast on Tuesday September 12, 2023 @06:05AM (#37478877)
Why would you log an error for an expected path? That should just be an info or debug level log, not error. In any case, it's really easy to disable errors either by line, module or project if really necessary.
Re:Flake8-Logging by teddyh on Tuesday September 12, 2023 @04:18PM (#37487581)
If you had said log.critical(), I might (might) have agreed with you. But a log.error() is not necessarily an error on the Python level, and should not necessarily spam a backtrace into the log.