Skip to content

Commit ac7aa83

Browse files
committed
Address review comments, including adding some documentation.
1 parent 8de3577 commit ac7aa83

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Doc/library/logging.handlers.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ need to override.
234234
return the same output every time for a given input, otherwise the
235235
rollover behaviour may not work as expected.
236236

237+
It's also worth noting that care should be taken when using a namer to
238+
preserve certain attributes in the filename which are used during rotation.
239+
For example, :class:`RotatingFileHandler` expects to have a set of log files
240+
whose names contain successive integers, so that rotation works as expected,
241+
and :class:`TimedRotatingFileHandler` deletes old log files (based on the
242+
``backupCount`` parameter passed to the handler's initializer) by determining
243+
the oldest files to delete. For this to happen, the filenames should be
244+
sortable using the date/time portion of the filename, and a namer needs to
245+
respect this. (If a namer is wanted that doesn't respect this scheme, it will
246+
need to be used in a subclass of :class:`TimedRotatingFileHandler` which
247+
overrides the :meth:`~TimedRotatingFileHandler.getFilesToDelete` method to
248+
fit in with the custom naming scheme.)
249+
237250
.. versionadded:: 3.3
238251

239252

@@ -443,6 +456,10 @@ timed intervals.
443456

444457
Outputs the record to the file, catering for rollover as described above.
445458

459+
.. method:: getFilesToDelete()
460+
461+
Returns a list of filenames which should be deleted as part of rollover. These
462+
are the absolute paths of the oldest backup log files written by the handler.
446463

447464
.. _socket-handler:
448465

Lib/logging/handlers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2021 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818
Additional handlers for the logging package for Python. The core package is
1919
based on PEP 282 and comments thereto in comp.lang.python.
2020
21-
Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2021 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
@@ -378,7 +378,8 @@ def getFilesToDelete(self):
378378
# Our files could be just about anything after custom naming, but
379379
# likely candidates are of the form
380380
# foo.log.DATETIME_SUFFIX or foo.DATETIME_SUFFIX.log
381-
if not fileName.startswith(baseName) and fileName.endswith(e) and len(fileName) > (plen + 1) and not fileName[plen+1].isdigit():
381+
if (not fileName.startswith(baseName) and fileName.endswith(e) and
382+
len(fileName) > (plen + 1) and not fileName[plen+1].isdigit()):
382383
continue
383384

384385
if fileName[:plen] == prefix:

Lib/test/test_logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2019 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2021 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -16,7 +16,7 @@
1616

1717
"""Test harness for the logging module. Run all tests.
1818
19-
Copyright (C) 2001-2019 Vinay Sajip. All Rights Reserved.
19+
Copyright (C) 2001-2021 Vinay Sajip. All Rights Reserved.
2020
"""
2121

2222
import logging
@@ -5460,7 +5460,7 @@ def test_compute_files_to_delete(self):
54605460
rotator.namer = lambda name: name.replace('.log', '') + '.log'
54615461
for t in times:
54625462
files.append('%s.%s.log' % (prefix, t))
5463-
# Open empty files
5463+
# Create empty files
54645464
for fn in files:
54655465
p = os.path.join(wd, fn)
54665466
with open(p, 'wb') as f:

0 commit comments

Comments
 (0)