[Python-checkins] cpython: Closes #24884: refactored WatchedFileHandler file reopening into a separate

vinay.sajip python-checkins at python.org
Thu Oct 1 15:55:18 EDT 2015


https://hg.python.org/cpython/rev/6d61b057c375
changeset:   98468:6d61b057c375
user:        Vinay Sajip <vinay_sajip at yahoo.co.uk>
date:        Thu Oct 01 20:54:41 2015 +0100
summary:
  Closes #24884: refactored WatchedFileHandler file reopening into a separate method, based on a suggestion and patch by Marian Horban.

files:
  Doc/library/logging.handlers.rst |  12 +++++++++---
  Lib/logging/handlers.py          |  15 ++++++++++++---
  2 files changed, 21 insertions(+), 6 deletions(-)


diff --git a/Doc/library/logging.handlers.rst b/Doc/library/logging.handlers.rst
--- a/Doc/library/logging.handlers.rst
+++ b/Doc/library/logging.handlers.rst
@@ -162,11 +162,17 @@
    first call to :meth:`emit`.  By default, the file grows indefinitely.
 
 
+   .. method:: reopenIfNeeded()
+
+      Checks to see if the file has changed.  If it has, the existing stream is
+      flushed and closed and the file opened again, typically as a precursor to
+      outputting the record to the file.
+
+
    .. method:: emit(record)
 
-      Outputs the record to the file, but first checks to see if the file has
-      changed.  If it has, the existing stream is flushed and closed and the
-      file opened again, before outputting the record to the file.
+      Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
+      reopen the file if it has changed.
 
 .. _base-rotating-handler:
 
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -440,11 +440,11 @@
             sres = os.fstat(self.stream.fileno())
             self.dev, self.ino = sres[ST_DEV], sres[ST_INO]
 
-    def emit(self, record):
+    def reopenIfNeeded(self):
         """
-        Emit a record.
+        Reopen log file if needed.
 
-        First check if the underlying file has changed, and if it
+        Checks if the underlying file has changed, and if it
         has, close the old stream and reopen the file to get the
         current stream.
         """
@@ -467,6 +467,15 @@
                 # open a new file handle and get new stat info from that fd
                 self.stream = self._open()
                 self._statstream()
+
+    def emit(self, record):
+        """
+        Emit a record.
+
+        If underlying file has changed, reopen the file before emitting the
+        record to it.
+        """
+        self.reopenIfNeeded()
         logging.FileHandler.emit(self, record)
 
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list