Course

Python Logging: Syntax, Usage, and Examples

Python logging is a built-in module that allows you to track events in your application. It helps you debug, monitor, and analyze errors by recording messages at different severity levels. Instead of using print(), logging provides a structured approach to capturing information about program execution.

How to Use Python Logging

The logging module provides a flexible system for creating logs. Here’s a basic example of setting up logging:

python
import logging # Configure logging logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s") # Log messages at different levels logging.debug("This is a debug message.") logging.info("This is an info message.") logging.warning("This is a warning message.") logging.error("This is an error message.") logging.critical("This is a critical message.")
  • basicConfig(): Configures the logging system.
  • level=logging.DEBUG: Sets the lowest severity level that will be logged.
  • format="%(levelname)s: %(message)s": Defines the output format.
  • debug(), info(), warning(), error(), critical(): Generate logs with different levels of severity.

When to Use Python Logging

Logging is essential for debugging, auditing, and performance monitoring. Here are some common scenarios where logging improves efficiency:

Debugging Applications

Logs help identify issues without modifying the code. You can trace function calls, variable values, and error occurrences.

python
import logging logging.basicConfig(level=logging.DEBUG) def divide(a, b): logging.debug(f"Dividing {a} by {b}") if b == 0: logging.error("Division by zero attempted.") return None return a / b result = divide(10, 0)

Saving Logs to a File

If you want to save logs for later analysis, you can write them to a file.

python
import logging logging.basicConfig(filename="app.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") logging.info("Application started")

Handling Errors in Web Applications

Web frameworks like Flask and Django use logging to track user actions and errors.

python
import logging logging.basicConfig(level=logging.ERROR) try: 1 / 0 except ZeroDivisionError: logging.exception("An error occurred")

Examples of Python Logging

Using a Custom Logger

Creating a custom logger allows more control over logging behavior.

python
import logging logger = logging.getLogger("custom_logger") logger.setLevel(logging.INFO) handler = logging.FileHandler("custom.log") formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) logger.info("Custom logger message")

Logging in a Multi-Threaded Program

In multi-threaded applications, logging ensures thread safety and helps debug concurrency issues.

python
import logging import threading logging.basicConfig(level=logging.DEBUG, format="%(threadName)s: %(message)s") def task(): logging.info("Thread is running") threads = [threading.Thread(target=task) for _ in range(3)] for thread in threads: thread.start() for thread in threads: thread.join()

Using Logging Filters

Filters allow you to exclude certain log messages based on conditions.

python
import logging class NoWarningsFilter(logging.Filter): def filter(self, record): return record.levelno < logging.WARNING logger = logging.getLogger() handler = logging.StreamHandler() handler.addFilter(NoWarningsFilter()) logger.addHandler(handler) logger.warning("This warning will not be logged.") logger.info("This info message will be logged.")

Learn More About Python Logging

Logging Levels and Their Uses

  • DEBUG: Detailed information for diagnosing problems.
  • INFO: General events in a program’s execution.
  • WARNING: Something unexpected happened, but the program continues.
  • ERROR: A problem occurred, but execution continues.
  • CRITICAL: A severe issue that may cause the program to stop.

Logging Exceptions

Instead of printing stack traces, logging exceptions makes debugging easier.

python
import logging try: open("nonexistent_file.txt") except FileNotFoundError: logging.exception("File not found error")

Formatting Logs

Using Formatter, you can customize log output.

python
import logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) logging.info("Formatted log message")

Filtering Sensitive Data

When logging user input or API responses, exclude sensitive data.

python
import logging logging.basicConfig(level=logging.INFO) password = "super_secret_password" masked_password = "*" * len(password) logging.info(f"User logged in with password: {masked_password}")

Logging to Multiple Outputs

You can log messages to both the console and a file.

python
import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) console_handler = logging.StreamHandler() file_handler = logging.FileHandler("app.log") formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) logger.debug("Logging to multiple outputs")

Using a Rotating Log File

For applications that generate large log files, use a rotating file handler to limit file size.

python
import logging from logging.handlers import RotatingFileHandler handler = RotatingFileHandler("app.log", maxBytes=1024, backupCount=3) logging.basicConfig(handlers=[handler], level=logging.INFO) for i in range(100): logging.info(f"Log entry {i}")

Logging Configuration with DictConfig

Instead of calling basicConfig(), you can configure logging with dictConfig().

python
import logging.config config = { "version": 1, "formatters": { "detailed": { "format": "%(asctime)s - %(levelname)s - %(message)s" } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "detailed" } }, "root": { "level": "DEBUG", "handlers": ["console"] } } logging.config.dictConfig(config) logging.info("Logging configured with dictConfig")

When to Use Logging Instead of Print

  • For debugging: Logging captures details without cluttering the console.
  • For production applications: Logs can be written to files and analyzed later.
  • For structured error handling: Logging helps track issues systematically.

Python logging is a powerful tool that helps you manage application events, debug issues, and store execution details. Whether you’re running a simple script or a large-scale application, logging improves visibility and helps maintain code reliability.