Home

RotatingFileHandler

RotatingFileHandler is a log handler in Python's standard logging library, provided by the logging.handlers module. It writes log records to a file and rotates the file when its size reaches a configured threshold, enabling continuous logging without unbounded file growth.

The handler is configured by parameters such as filename, mode, maxBytes, backupCount, encoding, and delay. filename

Rotation works as log records are emitted. When the current file length exceeds maxBytes, doRollover is invoked.

File naming typically uses suffixes appended to the base filename, such as .1, .2, etc. This makes

RotatingFileHandler is suitable for size-based log management within single-process or simple multi-threaded environments. For multi-process scenarios

is
the
path
to
the
log
file,
mode
defaults
to
append
('a'),
maxBytes
sets
the
rotation
threshold,
backupCount
determines
how
many
rotated
files
to
keep,
encoding
specifies
the
file
encoding,
and
delay
defers
file
creation
until
the
first
log
record
is
emitted.
If
maxBytes
is
less
than
or
equal
to
zero,
rotation
is
disabled.
If
backupCount
is
zero,
no
rotation
backups
are
kept.
The
existing
log
files
are
renamed
to
increment
their
suffix
numbers
(for
example,
filename.1,
filename.2,
and
so
on),
up
to
the
limit
defined
by
backupCount.
The
latest
log
file
is
then
created
anew
with
the
original
filename
to
continue
logging.
Older
backups
beyond
backupCount
are
removed,
preventing
uncontrolled
growth
of
log
files.
it
possible
to
retain
a
fixed
number
of
recent
logs
while
discarding
the
oldest
ones
as
new
ones
are
created.
or
time-based
rotation,
other
handlers
like
TimedRotatingFileHandler
or
alternative
logging
configurations
may
be
preferred.