October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Under the Hood of Python Logging: The 4 Core Building Blocks

Python logging passes each event as a LogRecord through loggers, filters, handlers, and formatters. Learn each role, follow a warning to its destination, and prevent duplicate output.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python logging is built from four parts: loggers create and classify events, filters apply additional decisions, handlers route records to destinations, and formatters shape their output. The event moves through these parts as a LogRecord. Understanding that route makes it easier to configure logging—and to avoid duplicate messages.

What are the four parts of Python logging?

The Python Logging HOWTO describes event information passing among loggers, handlers, filters, and formatters in a LogRecord. Each component has a distinct job:

As an Amazon Associate I earn from qualifying purchases.

  • Logger: the interface your code calls to create a record and decide whether it should proceed.
  • Filter: an optional rule that makes a more specific decision about a record and, in current API documentation, may modify it or return a replacement.
  • Handler: the route that sends an accepted record to a destination such as a console or file.
  • Formatter: the rule that turns a record into the text or other presentation emitted by a handler.

A concise mental model is: logger creates and classifies; filter refines; handler routes; formatter presents. The formatter does not choose where a record goes—that is the handler’s job. Python Logging HOWTO, Python 3.14.8 · Python logging API reference, Python 3.14.8

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is a logger in Python?

A logger is the object application code uses to report events with calls such as debug(), info(), warning(), error(), and critical(). It creates a LogRecord containing information about the event, applies its level and any filters attached to it, then passes qualifying records to its handlers.

Use names that follow your package

At module scope, the common pattern is logger = logging.getLogger(__name__). Because __name__ reflects the module’s dotted name, related modules form a logger hierarchy: for example, myapp.storage is below myapp. This lets an application configure logging centrally while code in individual modules uses its own named logger. If a logger has no explicitly assigned level, its effective level can come from an ancestor. Python Logging HOWTO, Python 3.14.8

Levels classify severity

The standard levels, from least to most severe, are DEBUG, INFO, WARNING, ERROR, and CRITICAL. They express operational meaning: diagnostic detail, normal confirmation, an unexpected condition that can continue, a failed operation, or a severe condition. The root logger’s default level is WARNING, so INFO and DEBUG messages are normally excluded until configuration lowers the threshold. A logger’s level determines whether a call is enabled at its point of origin; handler levels can impose an additional threshold later. Python Logging HOWTO, Python 3.14.8

What does a logging handler do?

A handler sends records to an output destination. The standard library provides handlers for common destinations, including streams (often the console), disk files, rotating files, sockets, and queues. A handler can have its own severity level and filters, so one destination can receive a different subset of records from another. Python Logging HOWTO, Python 3.14.8

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose destinations and thresholds deliberately

When an application has more than one handler, decide what each destination needs to receive and how its output should look. For example, the Logging Cookbook describes sending all severities to a file while sending errors and more severe records to the console. The important choices are the destination, severity threshold, format, and whether propagation will send the same record to another handler. Python Logging Cookbook, Python 3.10

How do Python logging filters and formatters work?

Filters refine which records pass

A filter provides a finer decision than severity alone. It may be attached to a logger or a handler. A logger filter is consulted for events logged on that logger; it is not automatically consulted for records originating from every descendant logger. A handler filter sees records that reach that handler. Current API documentation also allows filters to modify a record or return a replacement, rather than only accept or reject it. Python Logging HOWTO, Python 3.14.8 · Python logging API reference, Python 3.14.8

Formatters shape the emitted record

A formatter controls the final presentation at a handler. A format might show the severity, logger name, message, and—when useful—the time. Because formatting happens at the handler, different handlers can present the same record differently for different destinations.

How a warning travels from a module to its output

Consider a module calling logger.warning("Cache is unavailable") on a logger created with logging.getLogger(__name__). Its route is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Logger and level: the named logger creates a record and checks whether WARNING meets its effective level.
  2. Logger filters: filters attached to that logger can accept, reject, or—where supported—change the record.
  3. Handler routing: the logger offers the record to its handlers. If propagation is enabled, it also passes the record up the logger hierarchy to ancestor handlers.
  4. Handler checks: each handler that receives the record applies its own level and filters.
  5. Formatting and emission: a handler’s formatter determines the presentation, and the handler emits the result to its destination.

This trace follows the documented logging model; the actual destinations and formatting depend on the application’s configuration. Python Logging HOWTO, Python 3.14.8

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why does Python logging print duplicate messages?

A common cause is attaching handlers at both a child logger and one of its ancestors while propagation remains enabled. The child’s record can be emitted by its own handler and then again by a handler higher in the hierarchy. The API reference notes that, in general, a handler does not need to be attached at multiple points in the logger hierarchy. Put a handler at the appropriate level, or deliberately disable propagation when a logger needs a separate route. Python logging API reference, Python 3.14.8

How should you configure logging?

Use basicConfig for a straightforward setup

For a small script or uncomplicated application, logging.basicConfig() is a quick way to configure the root logger, including its level, message format, and console or file destination. If INFO or DEBUG output is needed, set a lower threshold as part of the configuration.

Use explicit configuration for more control

Applications with named loggers, multiple handlers, or more involved routing can configure logging through objects, fileConfig(), or dictionary-based dictConfig(). The HOWTO recommends dictionary configuration for new applications and deployments, but that does not mean every application needs to move beyond basicConfig(). Choose the approach that fits the configuration’s complexity. Python Logging HOWTO, Python 3.14.8 · Python logging API reference, Python 3.14.8

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.