diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 9840b7b0aeba88..a662f5af090645 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -28,6 +28,7 @@ import queue import threading import copy +import warnings # # Some constants... @@ -258,6 +259,11 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, raise ValueError("Invalid rollover interval specified: %s" % self.when) self.extMatch = re.compile(self.extMatch, re.ASCII) + if self.when == 'MIDNIGHT' and interval != 1: + msg = (f'An interval of {interval} is incompatible with ' + "when='MIDNIGHT'. Use when='D' (for days) " + 'if you want to use an interval other than 1.') + warnings.warn(msg, category=DeprecationWarning, stacklevel=2) self.interval = self.interval * interval # multiply by units requested # The following line added because the filename passed in could be a # path object (see Issue #27493), but self.baseFilename will be a string diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 33db5d6443f6fd..03036e011281e1 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -6028,6 +6028,16 @@ def test_compute_rollover_daily_attime(self): finally: rh.close() + def test_midnight_interval(self): + with warnings.catch_warnings(record=True) as w: + rh = logging.handlers.TimedRotatingFileHandler( + self.fn, encoding="utf-8", when='MIDNIGHT', interval=2, backupCount=0, + utc=True) + rh.close() + if w: + self.assertIs(w[0].category, DeprecationWarning) + self.assertIn('An interval of 2 is incompatible ', str(w[0].message)) + #@unittest.skipIf(True, 'Temporarily skipped while failures investigated.') def test_compute_rollover_weekly_attime(self): currentTime = int(time.time())