Closes #24884: refactored WatchedFileHandler file reopening into a separate method, based on a suggestion and patch by Marian Horban.
This commit is contained in:
parent
14b1b486ca
commit
29a1445136
@ -162,11 +162,17 @@ for this value.
|
|||||||
first call to :meth:`emit`. By default, the file grows indefinitely.
|
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)
|
.. method:: emit(record)
|
||||||
|
|
||||||
Outputs the record to the file, but first checks to see if the file has
|
Outputs the record to the file, but first calls :meth:`reopenIfNeeded` to
|
||||||
changed. If it has, the existing stream is flushed and closed and the
|
reopen the file if it has changed.
|
||||||
file opened again, before outputting the record to the file.
|
|
||||||
|
|
||||||
.. _base-rotating-handler:
|
.. _base-rotating-handler:
|
||||||
|
|
||||||
|
@ -440,11 +440,11 @@ class WatchedFileHandler(logging.FileHandler):
|
|||||||
sres = os.fstat(self.stream.fileno())
|
sres = os.fstat(self.stream.fileno())
|
||||||
self.dev, self.ino = sres[ST_DEV], sres[ST_INO]
|
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
|
has, close the old stream and reopen the file to get the
|
||||||
current stream.
|
current stream.
|
||||||
"""
|
"""
|
||||||
@ -467,6 +467,15 @@ class WatchedFileHandler(logging.FileHandler):
|
|||||||
# open a new file handle and get new stat info from that fd
|
# open a new file handle and get new stat info from that fd
|
||||||
self.stream = self._open()
|
self.stream = self._open()
|
||||||
self._statstream()
|
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)
|
logging.FileHandler.emit(self, record)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user