Skip to content

Commit e9b4ec6

Browse files
zhattarhadthedev
andauthored
pythongh-105623 Fix performance degradation in logging RotatingFileHandler (pythonGH-105887)
The check for whether the log file is a real file is expensive on NFS filesystems. This commit reorders the rollover condition checking to not do the file type check if the expected file size is less than the rotation threshold. Co-authored-by: Oleg Iarygin <[email protected]>
1 parent 0890ad7 commit e9b4ec6

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Lib/logging/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ def shouldRollover(self, record):
193193
Basically, see if the supplied record would cause the file to exceed
194194
the size limit we have.
195195
"""
196-
# See bpo-45401: Never rollover anything other than regular files
197-
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
198-
return False
199196
if self.stream is None: # delay was set...
200197
self.stream = self._open()
201198
if self.maxBytes > 0: # are we rolling over?
202199
msg = "%s\n" % self.format(record)
203200
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
204201
if self.stream.tell() + len(msg) >= self.maxBytes:
202+
# See bpo-45401: Never rollover anything other than regular files
203+
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
204+
return False
205205
return True
206206
return False
207207

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix performance degradation in
2+
:class:`logging.handlers.RotatingFileHandler`. Patch by Craig Robson.

0 commit comments

Comments
 (0)