Skip to content

Commit 2a68ed9

Browse files
miss-islingtonzhattarhadthedev
authored
[3.12] gh-105623 Fix performance degradation in logging RotatingFileHandler (GH-105887) (GH-121116)
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. (cherry picked from commit e9b4ec6) Co-authored-by: Craig Robson <[email protected]> Co-authored-by: Oleg Iarygin <[email protected]>
1 parent a5048ad commit 2a68ed9

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Lib/logging/handlers.py

+3-3
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

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)