On this page
logging.exception
Prev: logging.error | Up: logging | Next: logging.fatal
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.exception
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import exception
Signature & Overview
logging.exception(msg, *args, exc_info=True, **kwargs)
Log a message with severity 'ERROR' on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.
Usage
import io
import logging
buf = io.StringIO()
logger = logging.getLogger("demo.exc")
h = logging.StreamHandler(buf)
logger.addHandler(h)
try:
try:
1 / 0
except ZeroDivisionError:
logger.exception("division failed")
finally:
logger.removeHandler(h)
assert "ZeroDivisionError" in buf.getvalue()
Gotchas
- Call ONLY inside an active
excepthandler: Outside anexceptblock,exc_info=TrueappendsNoneType: None.
REPL Inspect
from logging import exception
help(exception)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/error Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/fatal Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib