Added optional delay argument to FileHandler and subclasses.

This commit is contained in:
Vinay Sajip 2008-01-24 12:37:33 +00:00
parent 97ef165a1d
commit 92aa2f8d6b
1 changed files with 14 additions and 11 deletions

View File

@ -53,13 +53,13 @@ class BaseRotatingHandler(logging.FileHandler):
Not meant to be instantiated directly. Instead, use RotatingFileHandler Not meant to be instantiated directly. Instead, use RotatingFileHandler
or TimedRotatingFileHandler. or TimedRotatingFileHandler.
""" """
def __init__(self, filename, mode, encoding=None): def __init__(self, filename, mode, encoding=None, delay=0):
""" """
Use the specified filename for streamed logging Use the specified filename for streamed logging
""" """
if codecs is None: if codecs is None:
encoding = None encoding = None
logging.FileHandler.__init__(self, filename, mode, encoding) logging.FileHandler.__init__(self, filename, mode, encoding, delay)
self.mode = mode self.mode = mode
self.encoding = encoding self.encoding = encoding
@ -84,7 +84,7 @@ class RotatingFileHandler(BaseRotatingHandler):
Handler for logging to a set of files, which switches from one file Handler for logging to a set of files, which switches from one file
to the next when the current file reaches a certain size. to the next when the current file reaches a certain size.
""" """
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None): def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
""" """
Open the specified file and use it as the stream for logging. Open the specified file and use it as the stream for logging.
@ -107,7 +107,7 @@ class RotatingFileHandler(BaseRotatingHandler):
""" """
if maxBytes > 0: if maxBytes > 0:
mode = 'a' # doesn't make sense otherwise! mode = 'a' # doesn't make sense otherwise!
BaseRotatingHandler.__init__(self, filename, mode, encoding) BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
self.maxBytes = maxBytes self.maxBytes = maxBytes
self.backupCount = backupCount self.backupCount = backupCount
@ -156,8 +156,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
If backupCount is > 0, when rollover is done, no more than backupCount If backupCount is > 0, when rollover is done, no more than backupCount
files are kept - the oldest ones are deleted. files are kept - the oldest ones are deleted.
""" """
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None): def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0):
BaseRotatingHandler.__init__(self, filename, 'a', encoding) BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
self.when = string.upper(when) self.when = string.upper(when)
self.backupCount = backupCount self.backupCount = backupCount
# Calculate the real rollover interval, which is just the number of # Calculate the real rollover interval, which is just the number of
@ -302,10 +302,13 @@ class WatchedFileHandler(logging.FileHandler):
This handler is based on a suggestion and patch by Chad J. This handler is based on a suggestion and patch by Chad J.
Schroeder. Schroeder.
""" """
def __init__(self, filename, mode='a', encoding=None): def __init__(self, filename, mode='a', encoding=None, delay=0):
logging.FileHandler.__init__(self, filename, mode, encoding) logging.FileHandler.__init__(self, filename, mode, encoding, delay)
stat = os.stat(self.baseFilename) if not os.path.exists(self.baseFilename):
self.dev, self.ino = stat[ST_DEV], stat[ST_INO] self.dev, self.ino = -1, -1
else:
stat = os.stat(self.baseFilename)
self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
def emit(self, record): def emit(self, record):
""" """
@ -321,7 +324,7 @@ class WatchedFileHandler(logging.FileHandler):
else: else:
stat = os.stat(self.baseFilename) stat = os.stat(self.baseFilename)
changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino) changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino)
if changed: if changed and self.stream is not None:
self.stream.flush() self.stream.flush()
self.stream.close() self.stream.close()
self.stream = self._open() self.stream = self._open()