Skip to content

error-instead-of-exception (TRY400)#

Derived from the tryceratops linter.

What it does#

Checks for uses of logging.error instead of logging.exception when logging an exception.

Why is this bad?#

logging.exception logs the exception and the traceback, while logging.error only logs the exception. The former is more appropriate when logging an exception, as the traceback is often useful for debugging.

Example#

import logging


def func():
    try:
        raise NotImplementedError
    except NotImplementedError:
        logging.error("Exception occurred")

Use instead:

import logging


def func():
    try:
        raise NotImplementedError
    except NotImplementedError:
        logging.exception("Exception occurred")

References#