import time import string """Simple logging module, maintains time and date stamps and wraps lines. $Revision: 1 $ Released into the public domain $Date: 10/28/04 12:04p $ Steve Holden http://www.holdenweb.com/ """ class logfile: """Logs strings with time and date stamps.""" def __init__(self, file="logfile.log", length=80): import os.path """Create log file with set line lengths and ouput file.""" self.file = open(file, "w") self.length = length if self.length < 40: self.length = 40 self.log("#### Log file %s ####\n" % (os.path.abspath(file), )) def log(self, s): """Log a line of text, wrapping as necessary to honor maximum line length.""" msg = "" m = "%s %s\n" % (time.strftime("%d-%m-%Y %H:%M:%S", time.localtime(time.time())), s) while len(m) > self.length: i = string.rfind(m, " ", 20, self.length) msg = msg + m[:i] + "\n" m = " "+m[i+1:] msg = msg+m self.file.write(msg) self.file.flush() def debug(self, s): """Log debugging data, tagging in log for easy identification.""" self.log("DEBUG: "+s) def close(self): """Terminate logging operations.""" self.file.close() # # Self test for simple service verification # if __name__ == "__main__": f = logfile("testlog.log") f.debug("One line of debug output") f.log("One line of non-debug output") f.close()