Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

logging

Python's logging module provides a flexible event-logging system for applications and libraries. Loggers create records that handlers route, filters select, and formatters lay out.

How it works

The module provides a flexible event-logging system. Application code sends records through loggers; handlers route them, filters decide which records pass, and formatters control their final layout.

Syntax

logging.getLogger(name)

Parameters

NameTypeDescription
namestrThe logger name used to obtain or identify a logger.

Returns

getLogger(name) returns a Logger object; makeLogRecord(attrdict) returns a LogRecord object.

Key points

  • The standard-library module supplies logging APIs for both application and library code.
  • Call logger methods from application code to record events.
  • Handlers are responsible for routing records to destinations such as streams or files.
  • Filters add detailed control over which records are allowed through.
  • Formatters determine how emitted records are represented.
  • Each logging operation normally produces a LogRecord automatically.

Common mistakes

  • Obtain loggers with getLogger(name) rather than constructing Logger objects yourself.
  • Logger names identify shared objects, so repeated lookups with the same name reuse the logger.

Example: Basic application logging

import logging

logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s:%(message)s")
logger = logging.getLogger(__name__)
logger.info("started")
logger.error("request failed")

Configures the root logger and emits messages through a module-specific logger.

When to use it

Application diagnostics

Record application events without building a logging framework.

Library observability

Let reusable packages emit events that host applications can configure.

Structured logging pipelines

Route records to different destinations with controlled selection and presentation.

Interview questions

QWhat are the principal components of Python's logging system?

Loggers are the interface application code calls. Handlers deliver logger-created records to destinations, filters refine which records are output, and formatters define the records' final layout.

Follow-up: How would you make different destinations use different layouts? Attach separate handlers and assign a formatter to each handler.

QHow should code acquire a logger?

Use logging.getLogger(name) to obtain a logger, because the documentation says loggers should not be instantiated directly. Reusing a name returns the same Logger object.

Follow-up: Why can a library and its caller coordinate logging? They can use named loggers in the same interpreter, allowing application configuration to handle library records.

QWhen are LogRecord objects created?

A LogRecord is created automatically by a Logger whenever something is logged. The module also permits creating one manually with makeLogRecord(), including for an event received over the wire.

Follow-up: When might manual creation be useful? It can reconstruct an event represented by received or serialized logging data before processing it.

  • Logger
  • Handler
  • Filter
  • Formatter
  • LogRecord
  • getLogger
  • makeLogRecord
  • basicConfig

Source: logging — Logging facility for Python. Checked 2026-10-16.