all Python 3loggingfunction
logging.exception()
levels
Logs a message at level ERROR on the root logger and appends the traceback of the exception being handled.
Syntax
logging.exception(msg, *args, exc_info=True, **kwargs)
It only makes sense inside an except block: outside one the log gets the line 'NoneType: None' instead of a traceback. Its level is plain ERROR — it is error(..., exc_info=True). It neither swallows nor re-raises anything: after logging the program carries on unless you write raise.
Examples
import logging, sys
logging.basicConfig(stream=sys.stdout, format='%(levelname)s %(message)s')
try:
1 / 0
except ZeroDivisionError:
logging.exception('сбой расчёта') # → ? ERROR сбой расчёта / Traceback ... / ZeroDivisionError: division by zero
import logging, sys
logging.basicConfig(stream=sys.stdout, format='%(message)s')
logging.exception('вне except') # → вне except / NoneType: None
See also
Open in the full glossarydocs.python.orgReport an inaccuracy