Skip to main content

How to Log an Error in Python with logger.error

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When something fails, your program should say so in its log, at a level that stands out from ordinary messages. logger.error writes a message at the ERROR level, and it can fill values into the message for you. This guide logs a failed payment, with real output from Python 3.15.2.

Syntax​

Syntax
logger.error("<message with %s placeholders>", <value>, ...)
PartWhat it means
"<message ...>"The text. Each %s is a placeholder for one of the values that follow
<value>, ...The values, in the order of the placeholders. logging fills them in only when the line is shown

The logging reference: "Logs a message with level ERROR on this logger. The arguments are interpreted as for debug()."

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

The script sets the line format, gets a logger named payments, and logs one error with the amount in it. It sets no level: ERROR is above the default WARNING, so it's shown anyway.

log_error.py
import logging

logging.basicConfig(format="%(levelname)s %(name)s: %(message)s")
logger = logging.getLogger("payments")

amount = 250
logger.error("Payment of %s failed: the card was declined.", amount)

The log_error.py script in a macOS-style code window: logging.basicConfig with a format, logger = logging.getLogger("payments"), amount = 250 and logger.error with a %s placeholder for the amount.

Step 2: Run It​

Terminal
uv run python log_error.py
Output
ERROR payments: Payment of 250 failed: the card was declined.

The script's output in a terminal: ERROR payments: Payment of 250 failed: the card was declined.

The %s became 250, and the line starts with ERROR.

Common Mistakes​

MistakeWhat happensDo this instead
A value without a %s for it, as in logger.error("Payment failed:", 250)logging prints --- Logging error --- and TypeError: not all arguments converted during string formatting (required 0, got 1) instead of your message. The program carries onPut one %s in the message for each value
Using logger.error inside except and losing the tracebackOnly your message is loggedUse logger.exception, below

Troubleshooting​

SymptomLikely causeFix
--- Logging error --- in the outputThe placeholders and the values don't matchCount the %s and the values
You need the line number of the failurelogger.error logs only the messagelogger.exception("...") inside the except adds the traceback

FAQ​

How do I log the traceback too?​

Call logger.exception in the except block. For a 1 / 0 caught as ZeroDivisionError, logger.exception("Could not split the bill.") logged ERROR payments: Could not split the bill. followed by the traceback ending in ZeroDivisionError: division by zero. The reference: "Exception info is added to the logging message. This method should only be called from an exception handler."

Why not build the message with an f-string?​

Both work. With placeholders, logging fills the values in only when the line is actually shown, which saves the work for lines that are filtered out.

Conclusion​

logger.error("Payment of %s failed: the card was declined.", amount) logged ERROR payments: Payment of 250 failed: the card was declined., and logger.exception adds the traceback when the error is an exception.

Additional Resources​