BUN2TOO.Insights / Technology

Technology · Analysis

Why your Python INFO messages disappear—and how logging levels work

Bun2too Editorial TeamOctober 5, 20263 min read
Editorial illustration for Why your Python INFO messages disappear—and how logging levels work
Original editorial artwork generated for this article

You add a message saying that your Python program has started, run the script, and see nothing. Then you add a warning, and it appears. Has the first message failed? Not necessarily: Python’s logging system filters events by severity, and its default threshold is WARNING.

Understanding that filter is a useful first step toward making software easier to investigate. For nontechnical readers, think of a log as a record of events that developers have chosen to report—not a complete recording of everything a program does.

What the five levels mean

Python’s Logging HOWTO describes logging as a way to track events while software runs. Each event has a message and a level indicating its importance. The standard levels, from lowest to highest severity, are:

• DEBUG: detailed diagnostic information, usually useful when investigating a problem.
• INFO: confirmation that things are working as expected.
• WARNING: something unexpected happened, or a problem may be approaching, but the software is still working as expected.
• ERROR: a more serious problem prevented the software from performing a function.
• CRITICAL: a serious error may prevent the program itself from continuing.

With the default WARNING threshold, INFO and DEBUG messages are filtered out. WARNING, ERROR and CRITICAL messages meet the threshold. A missing message therefore does not, by itself, tell you whether that part of the program ran.

Try an explicit configuration

Save the following as a fresh standalone Python script and run it. Using a new script avoids confusion from logging configuration already established in an interactive session.

import logging

logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO)

logger.debug('Checking startup details')
logger.info('Application started')
logger.warning('Example warning')

With this configuration, the expected output is:

INFO:__main__:Application started WARNING:__main__:Example warning

The DEBUG message stays hidden because it is below INFO. The INFO and WARNING messages appear because they meet or exceed the configured threshold. The example warning is illustrative, not evidence of an actual fault.

The HOWTO recommends configuring logging before calling logger methods. It also recommends creating loggers and controlling configuration explicitly in larger programs. Here, getLogger(__name__) creates a logger named for the module, while basicConfig establishes the basic logging setup.

Choose the right tool

Before adding another message, use this checklist:

• Is it ordinary output that someone using a command-line program needs to see? Use print().
• Is it an operational event worth recording for monitoring or investigation? Use logging, choosing a level that matches its importance.
• Is it an error that should interrupt the current operation? Raise an exception rather than assuming an ERROR log will do that for you.

Our practical recommendation is to decide which routine events deserve INFO messages, reserve DEBUG for diagnostic detail, and configure the threshold deliberately. Logging can provide useful evidence about software behavior, but only for events the developer reports. Clear messages and intentional levels make that evidence easier to interpret.