Skip to main content

How to Set Up Logging in Python with logging.basicConfig

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

print is fine for quick checks, but a real program needs messages with a level, such as information, warnings and errors, so you can choose which ones to show. Python's logging module does that, and logging.basicConfig sets it up in one line. This guide turns logging on for a small script, with real output from Python 3.15.2.

Syntax​

Syntax
import logging

logging.basicConfig(level=<lowest level shown>, format="<line layout>")
PartWhat it means
level=The lowest level that's shown: logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR or logging.CRITICAL. Without it, the level is WARNING.
format=The layout of each line. %(levelname)s is the level, %(name)s the logger's name and %(message)s the message.

The logging reference: "Does basic configuration for the logging system by either creating a StreamHandler with a default Formatter or using the given formatter instance, and adding it to the root logger."

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 turns logging on at the INFO level, then logs one message at each of three levels.

log_setup.py
import logging

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")

logging.info("The program started.")
logging.debug("This line is below the INFO level, so it isn't shown.")
logging.warning("Disk space is low.")

The log_setup.py script in a macOS-style code window: logging.basicConfig with level INFO and a format of level name, logger name and message, then logging.info, logging.debug and logging.warning calls.

  • level=logging.INFO: INFO and everything above it is shown; DEBUG is below it.
  • logging.info(...): logs through the root logger, the one basicConfig sets up. Its name is root.

Step 2: Run It​

Terminal
uv run python log_setup.py
Output
INFO root: The program started.
WARNING root: Disk space is low.

The script's output in a terminal: INFO root: The program started. and WARNING root: Disk space is low., with no debug line.

Two lines, in the format from Step 1. The debug message isn't there, because DEBUG is below the INFO level.

Common Mistakes​

MistakeWhat happensDo this instead
No basicConfig calllogging.info prints nothing: the default level is WARNING. Only WARNING:root:Disk space is low. appeared, in the default formatCall basicConfig with level=logging.INFO at the start
Logging before basicConfigThe first log call sets logging up with the defaults, and the later basicConfig does nothing: the info line stayed hidden and the format stayed WARNING:root:...Call basicConfig before the first log call

Troubleshooting​

SymptomLikely causeFix
A change to level= or format= has no effectSomething logged before basicConfig ranMove basicConfig to the top of the program
You need the messages in a filebasicConfig writes to the terminal by defaultAdd filename="app.log": the line INFO Saved to the file, not the screen. went into app.log instead

FAQ​

Where do the messages go?​

To the standard error stream, stderr, not to standard output. In a check, the handler basicConfig created wrote to sys.stderr. The terminal shows both streams, so you see them either way.

Why does basicConfig run on its own?​

The reference: "The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger." That's why a log call before your own basicConfig locks in the defaults.

Conclusion​

logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s") turned logging on: the script printed its info and warning lines in that layout, and left the debug line out.

Additional Resources​