added close and __del__

This commit is contained in:
Dave Schuyler 2004-05-21 02:03:41 +00:00
parent f33c8984f2
commit fa9a3929b8

View File

@ -10,10 +10,10 @@ class RotatingLog:
to a new file if the prior file is too large or after a time interval. to a new file if the prior file is too large or after a time interval.
""" """
def __init__(self, path="./log_file", timeInterval=24, megabyteLimit=1024): def __init__(self, path="./log_file", hourInterval=24, megabyteLimit=1024):
""" """
path is a full or partial path with file name. path is a full or partial path with file name.
timeInterval is the number of hours at which to rotate the file. hourInterval is the number of hours at which to rotate the file.
megabyteLimit is the number of megabytes of file size the log megabyteLimit is the number of megabytes of file size the log
may grow to, afterwhich the log is rotated. may grow to, afterwhich the log is rotated.
""" """
@ -21,12 +21,22 @@ class RotatingLog:
self.timeInterval=None self.timeInterval=None
self.timeLimit=None self.timeLimit=None
self.sizeLimit=None self.sizeLimit=None
if timeInterval is not None: if hourInterval is not None:
self.timeInterval=timeInterval*60*60 self.timeInterval=hourInterval*60*60
self.timeLimit=time.time()+self.timeInterval self.timeLimit=time.time()+self.timeInterval
if megabyteLimit is not None: if megabyteLimit is not None:
self.sizeLimit=megabyteLimit*1024*1024 self.sizeLimit=megabyteLimit*1024*1024
def __del__(self):
self.close()
def close(self):
print "close"
if hasattr(self, "file"):
self.file.flush()
self.file.close()
del self.file
def shouldRotate(self): def shouldRotate(self):
""" """
Returns a bool about whether a new log file should Returns a bool about whether a new log file should
@ -59,10 +69,7 @@ class RotatingLog:
path=self.filePath() path=self.filePath()
file=open(path, "a") file=open(path, "a")
if file: if file:
if hasattr(self, "file"): self.close()
self.file.flush()
self.file.close()
del self.file
self.file=file self.file=file
if self.timeLimit is not None and time.time() > self.timeLimit: if self.timeLimit is not None and time.time() > self.timeLimit:
self.timeLimit=time.time()+self.timeInterval self.timeLimit=time.time()+self.timeInterval