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.
Python code
24 linesimport 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
--- 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
- Use `TimedRotatingFileHandler` to rotate logs at set intervals (e.g., every day at midnight) instead of by size
- Combine multiple handlers (e.g., file + console) with `logger.addHandler()` to both log to disk and see output in the terminal
- 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
More from Errors & debugging
- Catch RecursionError and Fail Gracefully in Python easy
- Catch ValueError and print friendly message in Python easy
- Collect Multiple Validation Errors in Python Before Raising medium
- Handle ValueError and ZeroDivisionError in Python with try except easy
- How to Add a Correlation ID to Logging Records in Python medium
- How to Assert Preconditions with Descriptive Messages in Python easy
Keep learning
Related tutorials and quizzes for this topic.