Python log files can grow forever unless an application limits them. RotatingFileHandler solves one specific problem: when a log reaches a chosen size, Python can rename it as a backup and continue writing to a fresh file.
This lesson builds directly on OSPython.029: Logging to Files with FileHandler. If you need the logging levels first, review OSPython.028: Python Logging Basics.
Rotate A Log By File Size
RotatingFileHandler lives in logging.handlers. Two settings control the basic rotation behavior: maxBytes sets the approximate file size that triggers a rollover, and backupCount sets how many older files Python keeps.
import loggingfrom logging.handlers import RotatingFileHandlerlogger = logging.getLogger(__name__)logger.setLevel(logging.INFO)handler = RotatingFileHandler("app.log", maxBytes=1_000_000, backupCount=3)logger.addHandler(handler)logger.info("Application started")
In this example, app.log rolls over after it grows to roughly one megabyte. Python can then keep backup files such as app.log.1, app.log.2, and app.log.3 while the active application continues writing to a new app.log.
Understand maxBytes And backupCount
maxBytes answers, “How large may the active log become before rollover?” backupCount answers, “How many old rotated files should remain?” Together they stop a single application log from consuming storage without limit.
If backupCount=0, normal rollover backups are not retained. If the backup count is small, older generations disappear sooner as newer rotations occur. Choose limits based on how much diagnostic history you need and how much disk space the system can safely use.
Basic Troubleshooting
If rotation never happens, check whether the active file has actually reached maxBytes. Also confirm that the process has permission to create and rename files in the log directory.
If expected messages are missing, check both the logger level and handler level. Rotation controls file size and retention; it does not override severity filtering. Keep passwords, API keys, authentication tokens, and other secrets out of routine logs.
Exercise
Create a logger using RotatingFileHandler with maxBytes=200 and backupCount=2. Write enough INFO messages to force several rollovers. Inspect the directory and identify the active log plus its retained backups.
Knowledge Check
- What causes
RotatingFileHandlerto roll over? - What does
backupCountcontrol? - Does log rotation change severity filtering?
- Why use rotation on a long-running application?
Answers
- The active log reaching the configured
maxBytesthreshold. - How many older rotated log files are retained.
- No. Logger and handler levels still control which messages are written.
- To stop one continuously growing log file from consuming storage without limit.
Next Lesson
This lesson stops at size-based rotation. Time-based rotation with TimedRotatingFileHandler belongs in the next focused Python lesson.
References
Primary reference: Python documentation for RotatingFileHandler. The standard library defines size-based rollover through maxBytes and retained backups through backupCount.
BitcoinVersus.Tech
Editor’s Note
We volunteer daily to help keep the information on this platform verifiably accurate. If you would like to support our independent research, please donate here: 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb
BitcoinVersus.tech is not a financial advisor. Content is provided for informational purposes.

Leave a comment