Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

logging.exception

Logs at error severity and includes the currently handled exception information.

Version: Python 3.14; repository image, production runtime not attested

Syntax

logging.exception(msg, *args, exc_info=True, **kwargs)

Logs at error severity and includes the currently handled exception information.

Call it inside an exception handler to capture a meaningful traceback. The example tests for the exception marker without exposing unstable file or line details.

Inside except this root logger shortcut creates an ERROR record with exc_info=True by default. Formatting appends the active traceback and exception to the message without re-raising it. exc_info=False omits that information. Here both the text parse and the exception name ValueError appear.

import logging,io
o=io.StringIO()
logging.basicConfig(stream=o,format='%(levelname)s %(message)s',force=True)
try: int('x')
except ValueError:
    logging.exception('parse')
    assert 'ERROR parse' in o.getvalue() and 'Traceback' in o.getvalue() and 'ValueError' in o.getvalue()
    o.seek(0);o.truncate()
    logging.exception('short',exc_info=False)
assert o.getvalue()=='ERROR short\n'

Examples

import logging
from io import StringIO

out = StringIO()
logging.basicConfig(stream=out, force=True)
try:
    int("x")
except ValueError:
    logging.exception("parse")
print("ValueError" in out.getvalue(), "parse" in out.getvalue())

Result

True True

Inside except this root logger shortcut creates an ERROR record with exc_info=True by default. Formatting appends the active traceback and exception to the message without re-raising it. exc_info=False omits that information. Here both the text parse and the exception name ValueError appear.

Sources