all Python 3loggingfunction
logging.error()
levels
Writes a message at the ERROR level. Use it when part of the logic failed but the program keeps running.
Syntax
logging.error(msg, *args, exc_info=False, **kwargs)
Inside an except block prefer logging.exception(...) over logging.error(e): the level is the same ERROR, but the full traceback goes into the log (error(..., exc_info=True) does the same) — a bare str(e) like "division by zero" tells you nothing about where it happened. And logging is not handling: if you cannot continue, re-raise or return after the log call instead of carrying on as if nothing went wrong.
Examples
import logging
try:
result = 1 / 0
except ZeroDivisionError:
logging.error('Деление на ноль', exc_info=True) # + traceback
See also
Open in the full glossarydocs.python.orgReport an inaccuracy