Skip to content

stdlib/logging: Add full support for logging exception tracebacks.#660

Merged
dpgeorge merged 1 commit intomicropython:masterfrom
andrewleech:logging_exceptions
Jul 21, 2023
Merged

stdlib/logging: Add full support for logging exception tracebacks.#660
dpgeorge merged 1 commit intomicropython:masterfrom
andrewleech:logging_exceptions

Conversation

@andrewleech
Copy link
Copy Markdown
Contributor

I noticed recently that the current log.exception() function doesn't always print the tracebacks of the exception.

The current handling requires your build to be compiled with MICROPY_PY_SYS_EXC_INFO which is not always on by default.

This PR allows you to pass an exception object in as the exc_info kwarg (cpython allows this) so the following works regarless of the above build setting.

try:
    run_func()
except Exception as ex:
  log.exception("run_func failed", exc_info=ex)

Separately to that, currently even when sys.exc_info() is enabled, it's only printing the traceback to _stream = sys.stderr - not to the configured logging handlers. This means for instance if you've got a file log handler, it misses out on the tracebacks.
This PR also addresses this.

@dpgeorge
Copy link
Copy Markdown
Member

This looks good, thanks. And it's CPython compatible (CPython also allows exc_info to be a 3-tuple, but we don't need to support that).

This commit allows you to pass an exception object in as the exc_info kwarg
(CPython allows this), so logging exceptions can work even if the
MICROPY_PY_SYS_EXC_INFO option is disabled in the firmware.

Separately to that, currently even when sys.exc_info() is enabled, it's
only printing the traceback to _stream = sys.stderr - not to the configured
logging handlers.  This means for instance if you've got a file log
handler it misses out on the tracebacks.  That's also fixed in this commit.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@dpgeorge dpgeorge force-pushed the logging_exceptions branch from b152c5f to 5329ef5 Compare July 21, 2023 07:04
@dpgeorge dpgeorge merged commit 5329ef5 into micropython:master Jul 21, 2023
@andrewleech andrewleech deleted the logging_exceptions branch September 6, 2024 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants