5 import logging, logging.handlers
8 def __init__ (self,logfile,name=None,level=logging.INFO):
9 # default is to locate logger name from the logfile
11 name=os.path.basename(logfile)
12 self.logger=logging.getLogger(name)
13 self.logger.setLevel(level)
15 handler=logging.handlers.RotatingFileHandler(logfile,maxBytes=1000000, backupCount=5)
17 # This is usually a permissions error becaue the file is
18 # owned by root, but httpd is trying to access it.
19 tmplogfile=os.getenv("TMPDIR", "/tmp") + os.path.sep + os.path.basename(logfile)
20 handler=logging.handlers.RotatingFileHandler(tmplogfile,maxBytes=1000000, backupCount=5)
21 handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
22 self.logger.addHandler(handler)
25 def wrapped(self,msg,*args,**kwds):
26 native=getattr(self.logger,fun.__name__)
27 return native(msg,*args,**kwds)
28 #wrapped.__doc__=native.__doc__
31 def setLevel(self,level):
32 self.logger.setLevel(level)
45 # logs an exception - use in an except statement
46 def log_exc(self,message):
47 self.error("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n"))
48 self.error("%s END TRACEBACK"%message)
50 def log_exc_critical(self,message):
51 self.critical("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n"))
52 self.critical("%s END TRACEBACK"%message)
54 # for investigation purposes, can be placed anywhere
55 def log_stack(self,message):
56 to_log="".join(traceback.format_stack())
57 self.debug("%s BEG STACK"%message+"\n"+to_log)
58 self.debug("%s END STACK"%message)
60 sfa_logger=SfaLogging(logfile='/var/log/sfa.log')
61 sfa_import_logger=SfaLogging(logfile='/var/log/sfa_import.log')
64 ########################################
67 def profile(callable):
69 Prints the runtime of the specified callable. Use as a decorator, e.g.,
83 result = profile(foo)(...)
86 def wrapper(*args, **kwds):
88 result = callable(*args, **kwds)
91 args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()]
92 sfa_logger.debug("%s (%s): %.02f s" % (callable.__name__, ", ".join(args), end - start))
97 if __name__ == '__main__':
98 print 'testing sfalogging into logger.log'
100 sfa_logger=SfaLogging('logger.log')
101 sfa_logger.critical("logger.critical")
102 sfa_logger.error("logger.error")
103 sfa_logger.warning("logger.warning")
104 sfa_logger.info("logger.info")
105 sfa_logger.debug("logger.debug")
106 sfa_logger.setLevel(logging.DEBUG)
107 sfa_logger.debug("logger.debug again")
110 def sleep(seconds = 1):