Skip to content

Commit d3a1875

Browse files
committed
gh-105623 Fix performance degradation in logging RotatingFileHandler
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.
1 parent 101d5ec commit d3a1875

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

Lib/logging/handlers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ def shouldRollover(self, record):
187187
Basically, see if the supplied record would cause the file to exceed
188188
the size limit we have.
189189
"""
190-
# See bpo-45401: Never rollover anything other than regular files
191-
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
192-
return False
193190
if self.stream is None: # delay was set...
194191
self.stream = self._open()
195192
if self.maxBytes > 0: # are we rolling over?
196193
msg = "%s\n" % self.format(record)
197194
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
198195
if self.stream.tell() + len(msg) >= self.maxBytes:
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
199199
return True
200200
return False
201201

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix performance degradation in logging logging RotatingFileHandler.

0 commit comments

Comments
 (0)