- def setLevelFromOptVerbose(self,verbose):
- if verbose==0:
- self.logger.setLevel(logging.WARNING)
- elif verbose==1:
- self.logger.setLevel(logging.INFO)
- elif verbose==2:
- self.logger.setLevel(logging.DEBUG)
-
- ####################
- def wrap(fun):
- def wrapped(self,msg,*args,**kwds):
- native=getattr(self.logger,fun.__name__)
- return native(msg,*args,**kwds)
- #wrapped.__doc__=native.__doc__
- return wrapped
-
- @wrap
- def critical(): pass
- @wrap
- def error(): pass
- @wrap
- def warning(): pass
- @wrap
- def info(): pass
- @wrap
- def debug(): pass
-
- # logs an exception - use in an except statement
- def log_exc(self,message):
- self.error("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n"))
- self.error("%s END TRACEBACK"%message)
-
- def log_exc_critical(self,message):
- self.critical("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n"))
- self.critical("%s END TRACEBACK"%message)
-
- # for investigation purposes, can be placed anywhere
- def log_stack(self,message):
- to_log="".join(traceback.format_stack())
- self.debug("%s BEG STACK"%message+"\n"+to_log)
- self.debug("%s END STACK"%message)
-
-####################
-# import-related operations go in this file
-_import_logger=_SfaLogger(logfile='/var/log/sfa_import.log')
-# servers log into /var/log/sfa.log
-_server_logger=_SfaLogger(logfile='/var/log/sfa.log')
-# clients use the console
-_console_logger=_SfaLogger()
-
-# default is to use the server-side logger
-_the_logger=_server_logger
-
-# clients would change the default by issuing one of these call
-def sfa_logger_goes_to_console():
- current_module=sys.modules[globals()['__name__']]
- current_module._the_logger=_console_logger
-
-# clients would change the default by issuing one of these call
-def sfa_logger_goes_to_import():
- current_module=sys.modules[globals()['__name__']]
- current_module._the_logger=_import_logger
-
-# this is how to retrieve the 'right' logger
-def sfa_logger():
- return _the_logger
-
-########################################
-import time
-
-def profile(logger):
- """
- Prints the runtime of the specified callable. Use as a decorator, e.g.,
-
- @profile(logger)
- def foo(...):
- ...
- """
- def logger_profile(callable):
- def wrapper(*args, **kwds):
- start = time.time()
- result = callable(*args, **kwds)
- end = time.time()
- args = map(str, args)
- args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.iteritems()]
- # should probably use debug, but then debug is not always enabled
- logger.info("PROFILED %s (%s): %.02f s" % (callable.__name__, ", ".join(args), end - start))
- return result
- return wrapper
- return logger_profile
-
-
-if __name__ == '__main__':
- print 'testing sfalogging into logger.log'
- logger=_SfaLogger('logger.log')
- logger.critical("logger.critical")
- logger.error("logger.error")
- logger.warning("logger.warning")
- logger.info("logger.info")
- logger.debug("logger.debug")
- logger.setLevel(logging.DEBUG)
- logger.debug("logger.debug again")
-
- sfa_logger_goes_to_console()
- my_logger=sfa_logger()
- my_logger.info("redirected to console")
-
- @profile(my_logger)
- def sleep(seconds = 1):
- time.sleep(seconds)
-
- my_logger.info('console.info')
- sleep(0.5)
- my_logger.setLevel(logging.DEBUG)
- sleep(0.25)
+ def setLevelFromOptVerbose(self, verbose):
+ if verbose == 0:
+ self.setLevel(logging.WARNING)
+ elif verbose == 1:
+ self.setLevel(logging.INFO)
+ elif verbose >= 2:
+ self.setLevel(logging.DEBUG)
+
+ # in case some other code needs a boolean
+ @staticmethod
+ def getBoolVerboseFromOpt(verbose):
+ return verbose >= 1
+
+ @staticmethod
+ def getBoolDebugFromOpt(verbose):
+ return verbose >= 2
+
+ def log_exc(self, message, limit=100):
+ """
+ standard logger has an exception() method but this will
+ dump the stack only between the frames
+ (1) that does `raise` and (2) the one that does `except`
+
+ log_exc() has a limit argument that allows to see deeper than that
+
+ use limit=None to get the same behaviour as exception()
+ """
+ self.error("%s BEG TRACEBACK" % message + "\n" +
+ traceback.format_exc(limit=limit).strip("\n"))
+ self.error("%s END TRACEBACK" % message)