Back to index
A handler for logging to a file, which watches the file to see if it has changed while in use. This can happen because of usage of programs such as newsyslog and logrotate which perform log file rotation. This handler, intended for use under Unix, watches the file to see if it has changed since the last emit. (A file has changed if its device or inode have changed.) If it has changed, the old file stream is closed, and the file opened to get a new stream. This handler is not appropriate for use under Windows, because under Windows open files cannot be moved or renamed - logging opens the files with exclusive locks - and so there is no need for such a handler. Furthermore, ST_INO is not supported under Windows; stat always returns zero for this value. This handler is based on a suggestion and patch by Chad J. Schroeder.
00376 00377 def __init__(self, filename, mode='a', encoding=None, delay=0): 00378 logging.FileHandler.__init__(self, filename, mode, encoding, delay) 00379 if not os.path.exists(self.baseFilename): 00380 self.dev, self.ino = -1, -1 00381 else: 00382 stat = os.stat(self.baseFilename) 00383 self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
Emit a record. First check if the underlying file has changed, and if it has, close the old stream and reopen the file to get the current stream.
00384 00385 def emit(self, record): 00386 """ 00387 Emit a record. 00388 00389 First check if the underlying file has changed, and if it 00390 has, close the old stream and reopen the file to get the 00391 current stream. 00392 """ 00393 if not os.path.exists(self.baseFilename): 00394 stat = None 00395 changed = 1 00396 else: 00397 stat = os.stat(self.baseFilename) 00398 changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino) 00399 if changed and self.stream is not None: 00400 self.stream.flush() 00401 self.stream.close() 00402 self.stream = self._open() 00403 if stat is None: 00404 stat = os.stat(self.baseFilename) 00405 self.dev, self.ino = stat[ST_DEV], stat[ST_INO] 00406 logging.FileHandler.emit(self, record)