On this page
  1. logging.exception
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from logging import exception
help(exception)

Linked from

Related notes