How to Configure Python Logging with File Rotation

A complete demo that sets up a logger with a rotating file handler, writes several log entries, and shows the contents of the current log file.

Easy Python 3.9+ Aug 9, 2026 Errors & debugging 13 views 0 copies

Python code

24 lines
Python 3.9+
import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger("rotating_logger")
logger.setLevel(logging.DEBUG)

file_handler = RotatingFileHandler(
    "app.log",
    maxBytes=100,
    backupCount=3
)
file_handler.setFormatter(
    logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logger.addHandler(file_handler)

if __name__ == "__main__":
    for i in range(5):
        logger.info(f"Log entry number {i}")

    logger.info("Final message")
    with open("app.log", "r") as f:
        print("--- Current log file ---")
        print(f.read())

Output

stdout
--- Current log file ---
2025-01-15 10:30:45,123 - INFO - Log entry number 3
2025-01-15 10:30:45,123 - INFO - Log entry number 4
2025-01-15 10:30:45,123 - INFO - Final message

How it works

The RotatingFileHandler writes log records to a file and automatically rotates it when the file reaches maxBytes. With backupCount=3, the handler keeps up to three old log files (app.log.1, app.log.2, app.log.3) before deleting the oldest. Each log line is formatted with a timestamp, severity level, and message using the %(asctime)s - %(levelname)s - %(message)s format string. The logger.info calls write to the handler, and rotation happens transparently as the file grows past the size limit.

Common mistakes

  • Forgetting to call `logger.setLevel()` — the default WARNING level silently drops INFO messages
  • Using `maxBytes=0` or a negative value, which disables rotation entirely
  • Not closing or removing handlers in long-running apps, causing file handles to leak
  • Assuming rotation happens immediately — it only occurs on the next write after the size limit is exceeded

Variations

  1. Use `TimedRotatingFileHandler` to rotate logs at set intervals (e.g., every day at midnight) instead of by size
  2. Combine multiple handlers (e.g., file + console) with `logger.addHandler()` to both log to disk and see output in the terminal
  3. Set a custom `backupCount` and `encoding="utf-8"` on the handler for non-ASCII messages

Real-world use cases

  • Shipping application logs to a file that can grow indefinitely, using rotation to cap disk usage on production servers.
  • Debugging a background worker or cron job where console output is unavailable, with a rotating log capturing the last few events.
  • Building a service health-monitoring script that pages engineers when ERROR-level entries appear in rotated log files.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Errors & debugging

Related tutorials and quizzes for this topic.