@@ -51,13 +51,13 @@ class BaseRotatingHandler(logging.FileHandler):
5151 Not meant to be instantiated directly. Instead, use RotatingFileHandler
5252 or TimedRotatingFileHandler.
5353 """
54- def __init__ (self , filename , mode , encoding = None ):
54+ def __init__ (self , filename , mode , encoding = None , delay = 0 ):
5555 """
5656 Use the specified filename for streamed logging
5757 """
5858 if codecs is None :
5959 encoding = None
60- logging .FileHandler .__init__ (self , filename , mode , encoding )
60+ logging .FileHandler .__init__ (self , filename , mode , encoding , delay )
6161 self .mode = mode
6262 self .encoding = encoding
6363
@@ -82,7 +82,7 @@ class RotatingFileHandler(BaseRotatingHandler):
8282 Handler for logging to a set of files, which switches from one file
8383 to the next when the current file reaches a certain size.
8484 """
85- def __init__ (self , filename , mode = 'a' , maxBytes = 0 , backupCount = 0 , encoding = None ):
85+ def __init__ (self , filename , mode = 'a' , maxBytes = 0 , backupCount = 0 , encoding = None , delay = 0 ):
8686 """
8787 Open the specified file and use it as the stream for logging.
8888
@@ -105,7 +105,7 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None)
105105 """
106106 if maxBytes > 0 :
107107 mode = 'a' # doesn't make sense otherwise!
108- BaseRotatingHandler .__init__ (self , filename , mode , encoding )
108+ BaseRotatingHandler .__init__ (self , filename , mode , encoding , delay )
109109 self .maxBytes = maxBytes
110110 self .backupCount = backupCount
111111
@@ -154,8 +154,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
154154 If backupCount is > 0, when rollover is done, no more than backupCount
155155 files are kept - the oldest ones are deleted.
156156 """
157- def __init__ (self , filename , when = 'h' , interval = 1 , backupCount = 0 , encoding = None ):
158- BaseRotatingHandler .__init__ (self , filename , 'a' , encoding )
157+ def __init__ (self , filename , when = 'h' , interval = 1 , backupCount = 0 , encoding = None , delay = 0 ):
158+ BaseRotatingHandler .__init__ (self , filename , 'a' , encoding , delay )
159159 self .when = when .upper ()
160160 self .backupCount = backupCount
161161 # Calculate the real rollover interval, which is just the number of
@@ -300,10 +300,13 @@ class WatchedFileHandler(logging.FileHandler):
300300 This handler is based on a suggestion and patch by Chad J.
301301 Schroeder.
302302 """
303- def __init__ (self , filename , mode = 'a' , encoding = None ):
304- logging .FileHandler .__init__ (self , filename , mode , encoding )
305- stat = os .stat (self .baseFilename )
306- self .dev , self .ino = stat [ST_DEV ], stat [ST_INO ]
303+ def __init__ (self , filename , mode = 'a' , encoding = None , delay = 0 ):
304+ logging .FileHandler .__init__ (self , filename , mode , encoding , delay )
305+ if not os .path .exists (self .baseFilename ):
306+ self .dev , self .ino = - 1 , - 1
307+ else :
308+ stat = os .stat (self .baseFilename )
309+ self .dev , self .ino = stat [ST_DEV ], stat [ST_INO ]
307310
308311 def emit (self , record ):
309312 """
@@ -319,7 +322,7 @@ def emit(self, record):
319322 else :
320323 stat = os .stat (self .baseFilename )
321324 changed = (stat [ST_DEV ] != self .dev ) or (stat [ST_INO ] != self .ino )
322- if changed :
325+ if changed and self . stream is not None :
323326 self .stream .flush ()
324327 self .stream .close ()
325328 self .stream = self ._open ()
0 commit comments