From 1c3ac49602a984797aea72d9bcfd1ade04737d2c Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 30 Sep 2010 18:06:33 +0200 Subject: [PATCH] console_logger defined for the client side turn on verbosity if SFA_API_DEBUG is set --- sfa/plc/remoteshell.py | 4 +- sfa/plc/sfa-import-plc.py | 5 +- sfa/plc/sfaImport.py | 2 +- sfa/server/sfa-server.py | 1 + sfa/util/sfalogging.py | 116 +++++++++++++++++++------------------- 5 files changed, 66 insertions(+), 62 deletions(-) diff --git a/sfa/plc/remoteshell.py b/sfa/plc/remoteshell.py index 14cd629a..6ec59959 100644 --- a/sfa/plc/remoteshell.py +++ b/sfa/plc/remoteshell.py @@ -12,14 +12,16 @@ import xmlrpclib class RemoteShell: - def __init__(self): + def __init__(self,logger): self.servers = {} + self.logger=logger def call(self, name, pl_auth, *args): key = pl_auth["Url"] + "#" + pl_auth["Username"] if not (key in self.servers): + self.logger.info("Connecting to PLCAPI at url %s"%pl_auth['Url']) server = xmlrpclib.Server(pl_auth["Url"], verbose = 0, allow_none=True) #server.AdmAuthCheck(pl_auth) server.AuthCheck(pl_auth) diff --git a/sfa/plc/sfa-import-plc.py b/sfa/plc/sfa-import-plc.py index 084b90cd..814fffad 100755 --- a/sfa/plc/sfa-import-plc.py +++ b/sfa/plc/sfa-import-plc.py @@ -1,8 +1,5 @@ #!/usr/bin/python # -### $Id$ -### $URL$ -# ## # Import PLC records into the SFA database. It is indended that this tool be # run once to create SFA records that reflect the current state of the @@ -20,6 +17,7 @@ import getopt import sys import tempfile +import logging from sfa.util.record import * from sfa.util.table import SfaTable @@ -67,6 +65,7 @@ def main(): interface_hrn = config.SFA_INTERFACE_HRN keys_filename = config.config_path + os.sep + 'person_keys.py' sfaImporter = sfaImport() + if config.SFA_API_DEBUG: sfaImporter.logger.setLevel(logging.DEBUG) shell = sfaImporter.shell plc_auth = sfaImporter.plc_auth AuthHierarchy = sfaImporter.AuthHierarchy diff --git a/sfa/plc/sfaImport.py b/sfa/plc/sfaImport.py index 320730d4..c5169e8f 100644 --- a/sfa/plc/sfaImport.py +++ b/sfa/plc/sfaImport.py @@ -61,7 +61,7 @@ class sfaImport: self.shell = None if "Url" in self.plc_auth: from sfa.plc.remoteshell import RemoteShell - self.shell = RemoteShell() + self.shell = RemoteShell(self.logger) else: import PLC.Shell self.shell = PLC.Shell.Shell(globals = globals()) diff --git a/sfa/server/sfa-server.py b/sfa/server/sfa-server.py index 33779524..326db386 100755 --- a/sfa/server/sfa-server.py +++ b/sfa/server/sfa-server.py @@ -188,6 +188,7 @@ def main(): if options.verbose: sfa_logger.setLevel(logging.DEBUG) config = Config() + if config.SFA_API_DEBUG: sfa_logger.setLevel(logging.DEBUG) hierarchy = Hierarchy() server_key_file = os.path.join(hierarchy.basedir, "server.key") server_cert_file = os.path.join(hierarchy.basedir, "server.cert") diff --git a/sfa/util/sfalogging.py b/sfa/util/sfalogging.py index ccb14009..8f04a753 100755 --- a/sfa/util/sfalogging.py +++ b/sfa/util/sfalogging.py @@ -4,23 +4,33 @@ import os import traceback import logging, logging.handlers -class SfaLogging: - def __init__ (self,logfile,name=None,level=logging.INFO): - # default is to locate logger name from the logfile - if not name: - name=os.path.basename(logfile) - self.logger=logging.getLogger(name) - self.logger.setLevel(level) - try: - handler=logging.handlers.RotatingFileHandler(logfile,maxBytes=1000000, backupCount=5) - except IOError: - # This is usually a permissions error becaue the file is - # owned by root, but httpd is trying to access it. - tmplogfile=os.getenv("TMPDIR", "/tmp") + os.path.sep + os.path.basename(logfile) - handler=logging.handlers.RotatingFileHandler(tmplogfile,maxBytes=1000000, backupCount=5) +# a logger that can handle tracebacks +class _SfaLogger: + def __init__ (self,logfile=None,loggername=None,level=logging.INFO): + # default is to locate loggername from the logfile if avail. + if not logfile: + loggername='console' + handler=logging.StreamHandler() + else: + if not loggername: + loggername=os.path.basename(logfile) + try: + handler=logging.handlers.RotatingFileHandler(logfile,maxBytes=1000000, backupCount=5) + except IOError: + # This is usually a permissions error becaue the file is + # owned by root, but httpd is trying to access it. + tmplogfile=os.getenv("TMPDIR", "/tmp") + os.path.sep + os.path.basename(logfile) + handler=logging.handlers.RotatingFileHandler(tmplogfile,maxBytes=1000000, backupCount=5) + + self.logger=logging.getLogger(loggername) handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) + self.logger.setLevel(level) self.logger.addHandler(handler) + def setLevel(self,level): + self.logger.setLevel(level) + + #################### def wrap(fun): def wrapped(self,msg,*args,**kwds): native=getattr(self.logger,fun.__name__) @@ -28,9 +38,6 @@ class SfaLogging: #wrapped.__doc__=native.__doc__ return wrapped - def setLevel(self,level): - self.logger.setLevel(level) - @wrap def critical(): pass @wrap @@ -57,57 +64,52 @@ class SfaLogging: self.debug("%s BEG STACK"%message+"\n"+to_log) self.debug("%s END STACK"%message) -sfa_logger=SfaLogging(logfile='/var/log/sfa.log') -sfa_import_logger=SfaLogging(logfile='/var/log/sfa_import.log') - +sfa_logger=_SfaLogger(logfile='/var/log/sfa.log') +sfa_import_logger=_SfaLogger(logfile='/var/log/sfa_import.log') +console_logger=_SfaLogger() ######################################## import time -def profile(callable): +def profile(logger): """ Prints the runtime of the specified callable. Use as a decorator, e.g., - - @profile - def foo(...): - ... - - Or, equivalently, - - def foo(...): - ... - foo = profile(foo) - - Or inline: - - result = profile(foo)(...) + + @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.items()] + # 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 - 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.items()] - sfa_logger.debug("%s (%s): %.02f s" % (callable.__name__, ", ".join(args), end - start)) - return result - - return wrapper if __name__ == '__main__': print 'testing sfalogging into logger.log' - global sfa_logger - sfa_logger=SfaLogging('logger.log') - sfa_logger.critical("logger.critical") - sfa_logger.error("logger.error") - sfa_logger.warning("logger.warning") - sfa_logger.info("logger.info") - sfa_logger.debug("logger.debug") - sfa_logger.setLevel(logging.DEBUG) - sfa_logger.debug("logger.debug again") - - @profile + 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") + + @profile(console_logger) def sleep(seconds = 1): time.sleep(seconds) - sleep(1) + + console_logger.info('console.info') + sleep(0.5) + console_logger.setLevel(logging.DEBUG) + sleep(0.25) -- 2.43.0