[Python] How to log a Python 3 exception, but without its stack trace?

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    When I want to log some specific Exception, but otherwise ignore it, I can do that like so:

    import logging

    logger = logging.getLogger(__name__)

    try:
    something_that_may_fail()
    except NameError:
    logger.error("It failed with:", exc_info=True)


    (This is in fact an MRE, as something_that_may_fail hasn't been defined, so the try block will raise NameError with message name 'something_that_may_fail' is not defined. )

    This however will also log the stack trace:

    It failed with:
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    NameError: name 'something_that_may_fail' is not defined


    Sometimes that isn't what I want: In some cases, I already know that exception type and exception message (together with my custom log message) will suffice, and don't want to expand the log with stack traces that don't tell me anything new. So I'd want a log entry that simply is

    It failed with:
    NameError: name 'something_that_may_fail' is not defined


    I can achieve that by passing a 3-tuple as the exc_info, with the stack trace component replaced by None:

    import logging
    import sys

    logger = logging.getLogger(__name__)

    try:
    something_that_may_fail()
    except NameError:
    exc_type, exc_value, _trace = sys.exc_info()
    logger.error("It failed with:", exc_info=(exc_type, exc_value, None))


    But I'm not sure how reliable that is. (The documentation doesn't mention how the tuple may or may not deviate from one returned by sys.exc_info().)

    Examining the exception myself with

    ...
    except NameError as e:
    ...


    comes with its own problems:

    • f"{type(e)}" gives string <class 'NameError'> instead of just string NameError
    • Can I rely on the message always being e.args[0]? (I might have uses for other exceptions (with more sub-types) than just NameError, which I've used here only as an example.)

    So what is the proper way to log exception type and message without the stack trace? Is there a cleaner way than my make-the-trace-None hack above?

    Continue reading...

Compartilhe esta Página