Skip to content

Commit ec9d12a

Browse files
Fix for #68 when using Python < 3.9
Use the version of `TimedRotatingFileHandler.getFilesToDelete` from Python 3.11 when operating under Python 3.8 or earlier. This handles the `.1` type extensions when the file rolls over multiple times during the normal time interval. Otherwise these types of files never get deleted when `backupCount` is set.
1 parent fe77772 commit ec9d12a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/concurrent_log_handler/__init__.py

+53
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,59 @@ def doRollover(self): # noqa: C901, PLR0912
861861
self.write_rollover_time()
862862
self._console_log(f"Rotation completed (on time) {dfn}")
863863

864+
def getFilesToDelete(self):
865+
"""
866+
Determine the files to delete when rolling over.
867+
868+
Copied from Python 3.11, and only applied when the current Python
869+
seems to be Python 3.8 or lower, which is when this seemed to change.
870+
The newer version supports custom suffixes like ours, such
871+
as when hitting a size limit before the time limit.
872+
"""
873+
874+
# If Python > 3.8, then use the superclass method.
875+
if sys.version_info >= (3, 9):
876+
return super().getFilesToDelete()
877+
878+
dirName, baseName = os.path.split(self.baseFilename)
879+
fileNames = os.listdir(dirName)
880+
result = []
881+
# See bpo-44753: Don't use the extension when computing the prefix.
882+
n, e = os.path.splitext(baseName)
883+
prefix = n + "."
884+
plen = len(prefix)
885+
for fileName in fileNames:
886+
if self.namer is None:
887+
# Our files will always start with baseName
888+
if not fileName.startswith(baseName):
889+
continue
890+
# Our files could be just about anything after custom naming, but
891+
# likely candidates are of the form
892+
# foo.log.DATETIME_SUFFIX or foo.DATETIME_SUFFIX.log
893+
elif (
894+
not fileName.startswith(baseName)
895+
and fileName.endswith(e)
896+
and len(fileName) > (plen + 1)
897+
and not fileName[plen + 1].isdigit()
898+
):
899+
continue
900+
901+
if fileName[:plen] == prefix:
902+
suffix = fileName[plen:]
903+
# See bpo-45628: The date/time suffix could be anywhere in the
904+
# filename
905+
parts = suffix.split(".")
906+
for part in parts:
907+
if self.extMatch.match(part):
908+
result.append(os.path.join(dirName, fileName))
909+
break
910+
if len(result) < self.backupCount:
911+
result = []
912+
else:
913+
result.sort()
914+
result = result[: len(result) - self.backupCount]
915+
return result
916+
864917

865918
# Publish these classes to the "logging.handlers" module, so they can be used
866919
# from a logging config file via logging.config.fileConfig().

0 commit comments

Comments
 (0)