How to Log an Error in Python with logger.error
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
logger.error("<message with %s placeholders>", <value>, ...)
| Part | What 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.
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)

Step 2: Run It
uv run python log_error.py
ERROR payments: Payment of 250 failed: the card was declined.

The %s became 250, and the line starts with ERROR.
Common Mistakes
| Mistake | What happens | Do 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 on | Put one %s in the message for each value |
Using logger.error inside except and losing the traceback | Only your message is logged | Use logger.exception, below |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
--- Logging error --- in the output | The placeholders and the values don't match | Count the %s and the values |
| You need the line number of the failure | logger.error logs only the message | logger.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
- Logger.error — the method and its arguments.
- Logger.exception — logging an error with its traceback.
