From 69c8781cec5778d5a5d8a3c485cbb1974e41ac99 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sat, 23 Jan 2021 21:04:03 +0100 Subject: [PATCH] Fix log rotation in Red (#4738) * Fix log rotation in Red * Update regexes to only work with single digit suffixes --- redbot/logging.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/redbot/logging.py b/redbot/logging.py index 9ae895723..694d91e4c 100644 --- a/redbot/logging.py +++ b/redbot/logging.py @@ -58,7 +58,7 @@ class RotatingFileHandler(logging.handlers.RotatingFileHandler): self.baseStem = stem self.directory = directory.resolve() # Scan for existing files in directory, append to last part of existing log - log_part_re = re.compile(rf"{stem}-part(?P\d+).log") + log_part_re = re.compile(rf"{stem}-part(?P\d)\.log") highest_part = 0 for path in directory.iterdir(): match = log_part_re.match(path.name) @@ -86,7 +86,7 @@ class RotatingFileHandler(logging.handlers.RotatingFileHandler): initial_path.replace(self.directory / f"{self.baseStem}-part1.log") match = re.match( - rf"{self.baseStem}(?:-part(?P\d+)?)?.log", pathlib.Path(self.baseFilename).name + rf"{self.baseStem}(?:-part(?P\d))?\.log", pathlib.Path(self.baseFilename).name ) latest_part_num = int(match.groupdict(default="1").get("part", "1")) if self.backupCount < 1: @@ -95,7 +95,7 @@ class RotatingFileHandler(logging.handlers.RotatingFileHandler): elif latest_part_num > self.backupCount: # Rotate files down one # red-part2.log becomes red-part1.log etc, a new log is added at the end. - for i in range(1, self.backupCount): + for i in range(1, self.backupCount + 1): next_log = self.directory / f"{self.baseStem}-part{i + 1}.log" if next_log.exists(): prev_log = self.directory / f"{self.baseStem}-part{i}.log"