From db091e73c33c373b7f6c2c96bd2caf6a2acf0178 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Tue, 21 Jun 2011 16:52:35 -0400 Subject: [PATCH] really fixed the redundant logging issue this time. --- sfa/client/sfadump.py | 4 +- sfa/client/sfascan.py | 22 +++---- sfa/client/sfi.py | 4 +- sfa/managers/aggregate_manager_max.py | 8 +-- sfa/managers/slice_manager_pl.py | 4 +- sfa/plc/api.py | 5 +- sfa/plc/network.py | 1 - sfa/plc/sfa-import-plc.py | 3 +- sfa/plc/sfa-nuke-plc.py | 9 ++- sfa/plc/sfaImport.py | 5 +- sfa/rspecs/rspec_version.py | 3 +- sfa/server/interface.py | 1 - sfa/server/modpython/SfaAggregateModPython.py | 4 +- sfa/server/modpython/SfaRegistryModPython.py | 4 +- sfa/server/modpython/SfaSliceMgrModPython.py | 4 +- sfa/server/sfa-server.py | 19 +++--- sfa/trust/auth.py | 6 +- sfa/trust/certificate.py | 22 +++---- sfa/trust/credential.py | 6 +- sfa/trust/gid.py | 4 +- sfa/trust/hierarchy.py | 9 ++- sfa/util/PostgreSQL.py | 20 +++--- sfa/util/api.py | 8 +-- sfa/util/callids.py | 16 ++--- sfa/util/componentserver.py | 4 +- sfa/util/method.py | 8 +-- sfa/util/rspec.py | 6 +- sfa/util/rspecHelper.py | 10 +-- sfa/util/server.py | 10 +-- sfa/util/sfalogging.py | 64 +++++++------------ sfa/util/xmlrpcprotocol.py | 6 +- sfa/util/xrn.py | 3 +- 32 files changed, 137 insertions(+), 165 deletions(-) diff --git a/sfa/client/sfadump.py b/sfa/client/sfadump.py index 1ea1a7c0..54654f84 100755 --- a/sfa/client/sfadump.py +++ b/sfa/client/sfadump.py @@ -13,7 +13,7 @@ from sfa.trust.credential import Credential from sfa.trust.gid import GID from sfa.util.record import SfaRecord from sfa.util.rspec import RSpec -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger def determine_sfa_filekind(fn): @@ -110,7 +110,7 @@ display info on input files""" parser.add_option("-v", "--verbose", action='count', dest='verbose', default=0) (options, args) = parser.parse_args() - sfa_logger().setLevelFromOptVerbose(options.verbose) + logger.setLevelFromOptVerbose(options.verbose) if len(args) <= 0: parser.print_help() sys.exit(1) diff --git a/sfa/client/sfascan.py b/sfa/client/sfascan.py index eff17ec0..b6f62e29 100755 --- a/sfa/client/sfascan.py +++ b/sfa/client/sfascan.py @@ -10,7 +10,7 @@ import pygraphviz from optparse import OptionParser from sfa.client.sfi import Sfi -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger import sfa.util.xmlrpcprotocol as xmlrpcprotocol def url_hostname_port (url): @@ -68,7 +68,7 @@ class Interface: key_file = client.get_key_file() cert_file = client.get_cert_file(key_file) url=self.url() - sfa_logger().info('issuing get version at %s'%url) + logger.info('issuing get version at %s'%url) server=xmlrpcprotocol.get_server(url, key_file, cert_file, options) self._version=server.GetVersion() except: @@ -158,17 +158,17 @@ class SfaScan: # performing xmlrpc call version=interface.get_version() if self.verbose: - sfa_logger().info("GetVersion at interface %s"%interface.url()) + logger.info("GetVersion at interface %s"%interface.url()) if not version: - sfa_logger().info("") + logger.info("") else: for (k,v) in version.iteritems(): if not isinstance(v,dict): - sfa_logger().info("\r\t%s:%s"%(k,v)) + logger.info("\r\t%s:%s"%(k,v)) else: - sfa_logger().info(k) + logger.info(k) for (k1,v1) in v.iteritems(): - sfa_logger().info("\r\t\t%s:%s"%(k1,v1)) + logger.info("\r\t\t%s:%s"%(k1,v1)) # 'geni_api' is expected if the call succeeded at all # 'peers' is needed as well as AMs typically don't have peers if 'geni_api' in version and 'peers' in version: @@ -195,7 +195,7 @@ class SfaScan: for (k,v) in interface.get_layout().iteritems(): node.attr[k]=v else: - sfa_logger().error("MISSED interface with node %s"%node) + logger.error("MISSED interface with node %s"%node) default_outfiles=['sfa.png','sfa.svg','sfa.dot'] @@ -218,12 +218,12 @@ def main(): scanner=SfaScan(left_to_right=options.left_to_right, verbose=options.verbose) entries = [ Interface(entry) for entry in args ] g=scanner.graph(entries) - sfa_logger().info("creating layout") + logger.info("creating layout") g.layout(prog='dot') for outfile in options.outfiles: - sfa_logger().info("drawing in %s"%outfile) + logger.info("drawing in %s"%outfile) g.draw(outfile) - sfa_logger().info("done") + logger.info("done") if __name__ == '__main__': main() diff --git a/sfa/client/sfi.py b/sfa/client/sfi.py index 09deb3ea..750d873b 100755 --- a/sfa/client/sfi.py +++ b/sfa/client/sfi.py @@ -15,7 +15,7 @@ from lxml import etree from StringIO import StringIO from types import StringTypes, ListType from optparse import OptionParser -from sfa.util.sfalogging import _SfaLogger, logging +from sfa.util.sfalogging import info_logger from sfa.trust.certificate import Keypair, Certificate from sfa.trust.gid import GID from sfa.trust.credential import Credential @@ -141,7 +141,7 @@ class Sfi: self.user = None self.authority = None self.hashrequest = False - self.logger = _SfaLogger(self.sfi_dir + 'sfi.log', level = logging.INFO) + self.logger = info_logger def create_cmd_parser(self, command, additional_cmdargs=None): cmdargs = {"list": "authority", diff --git a/sfa/managers/aggregate_manager_max.py b/sfa/managers/aggregate_manager_max.py index 0c374b41..36afd11c 100644 --- a/sfa/managers/aggregate_manager_max.py +++ b/sfa/managers/aggregate_manager_max.py @@ -2,7 +2,7 @@ from sfa.util.xrn import urn_to_hrn, hrn_to_urn, get_authority from sfa.util.plxrn import hrn_to_pl_slicename from sfa.util.plxrn import hrn_to_pl_slicename from sfa.util.rspec import RSpec -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.util.config import Config from sfa.managers.aggregate_manager_pl import GetVersion, __get_registry_objects from sfa.plc.slices import Slices @@ -38,7 +38,7 @@ def call_am_apiclient(client_app, params, timeout): (client_path, am_url) = Config().get_max_aggrMgr_info() sys_cmd = "cd " + client_path + "; java -classpath AggregateWS-client-api.jar:lib/* net.geni.aggregate.client.examples." + client_app + " ./repo " + am_url + " " + ' '.join(params) ret = shell_execute(sys_cmd, timeout) - sfa_logger().debug("shell_execute cmd: %s returns %s" % (sys_cmd, ret)) + logger.debug("shell_execute cmd: %s returns %s" % (sys_cmd, ret)) # save request RSpec xml content to a tmp file def save_rspec_to_file(rspec): path = RSPEC_TMP_FILE_PREFIX + "_" + time.strftime('%Y%m%dT%H:%M:%S', time.gmtime(time.time())) +".xml" @@ -105,9 +105,9 @@ def get_rspec(api, cred, options): rspec = " No resource found " else: comp_rspec = get_xml_by_tag(output, 'computeResource') - sfa_logger().debug("#### computeResource %s" % comp_rspec) + logger.debug("#### computeResource %s" % comp_rspec) topo_rspec = get_xml_by_tag(output, 'topology') - sfa_logger().debug("#### topology %s" % topo_rspec) + logger.debug("#### topology %s" % topo_rspec) rspec = " "; if comp_rspec != None: rspec = rspec + get_xml_by_tag(output, 'computeResource') diff --git a/sfa/managers/slice_manager_pl.py b/sfa/managers/slice_manager_pl.py index 1d801e38..5ed2261e 100644 --- a/sfa/managers/slice_manager_pl.py +++ b/sfa/managers/slice_manager_pl.py @@ -7,7 +7,7 @@ from copy import deepcopy from copy import copy from lxml import etree -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.util.rspecHelper import merge_rspecs from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn from sfa.util.plxrn import hrn_to_pl_slicename @@ -62,7 +62,7 @@ def get_serverproxy_url (server): try: return server.url except: - sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals") + logger.warning("GetVersion, falling back to xmlrpclib.ServerProxy internals") return server._ServerProxy__host + server._ServerProxy__handler def GetVersion(api): diff --git a/sfa/plc/api.py b/sfa/plc/api.py index 8e15ea79..200d05c5 100644 --- a/sfa/plc/api.py +++ b/sfa/plc/api.py @@ -12,7 +12,7 @@ import xmlrpclib from sfa.util.faults import * from sfa.util.api import * from sfa.util.config import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger import sfa.util.xmlrpcprotocol as xmlrpcprotocol from sfa.trust.auth import Auth from sfa.trust.rights import Right, Rights, determine_rights @@ -106,7 +106,6 @@ class SfaAPI(BaseAPI): self.hrn = self.config.SFA_INTERFACE_HRN self.time_format = "%Y-%m-%d %H:%M:%S" - self.logger=sfa_logger() def getPLCShell(self): self.plauth = {'Username': self.config.SFA_PLC_USER, @@ -340,7 +339,7 @@ class SfaAPI(BaseAPI): # fill in key info if record['type'] == 'user': if 'key_ids' not in record: - self.logger.info("user record has no 'key_ids' - need to import from myplc ?") + logger.info("user record has no 'key_ids' - need to import from myplc ?") else: pubkeys = [keys[key_id]['key'] for key_id in record['key_ids'] if key_id in keys] record['keys'] = pubkeys diff --git a/sfa/plc/network.py b/sfa/plc/network.py index 203d321e..9276fb0f 100644 --- a/sfa/plc/network.py +++ b/sfa/plc/network.py @@ -7,7 +7,6 @@ from lxml import etree from xmlbuilder import XMLBuilder from sfa.util.faults import * -#from sfa.util.sfalogging import sfa_logger from sfa.util.xrn import get_authority from sfa.util.plxrn import hrn_to_pl_slicename, hostname_to_urn diff --git a/sfa/plc/sfa-import-plc.py b/sfa/plc/sfa-import-plc.py index 7b96c359..45386ea6 100755 --- a/sfa/plc/sfa-import-plc.py +++ b/sfa/plc/sfa-import-plc.py @@ -28,6 +28,7 @@ from sfa.trust.trustedroot import * from sfa.trust.hierarchy import * from sfa.util.xrn import Xrn from sfa.plc.api import * +from sfa.util.sfalogging import logger from sfa.trust.gid import create_uuid from sfa.plc.sfaImport import sfaImport @@ -146,7 +147,7 @@ def main(): # start importing for site in sites: site_hrn = interface_hrn + "." + site['login_base'] - sfa_logger().info("Importing site: %s" % site_hrn) + logger.info("Importing site: %s" % site_hrn) # import if hrn is not in list of existing hrns or if the hrn exists # but its not a site record diff --git a/sfa/plc/sfa-nuke-plc.py b/sfa/plc/sfa-nuke-plc.py index 84360355..7ba63372 100755 --- a/sfa/plc/sfa-nuke-plc.py +++ b/sfa/plc/sfa-nuke-plc.py @@ -14,7 +14,7 @@ from optparse import OptionParser from sfa.trust.hierarchy import * from sfa.util.record import * from sfa.util.table import SfaTable -from sfa.util.sfalogging import sfa_logger_goes_to_import,sfa_logger +from sfa.util.sfalogging import logger def main(): usage="%prog: trash the registry DB (the 'sfa' table in the 'planetlab5' database)" @@ -25,8 +25,7 @@ def main(): if args: parser.print_help() sys.exit(1) - sfa_logger_goes_to_import() - sfa_logger().info("Purging SFA records from database") + logger.info("Purging SFA records from database") table = SfaTable() table.sfa_records_purge() if options.clean_fs: @@ -38,8 +37,8 @@ def main(): path=dir+os.sep+file os.unlink(path) if not os.path.exists(path): - sfa_logger().info("Unlinked file %s"%path) + logger.info("Unlinked file %s"%path) else: - sfa_logger().error("Could not unlink file %s"%path) + logger.error("Could not unlink file %s"%path) if __name__ == "__main__": main() diff --git a/sfa/plc/sfaImport.py b/sfa/plc/sfaImport.py index 238b5e1b..ee34833b 100644 --- a/sfa/plc/sfaImport.py +++ b/sfa/plc/sfaImport.py @@ -12,7 +12,7 @@ import getopt import sys import tempfile -from sfa.util.sfalogging import sfa_logger_goes_to_import,sfa_logger +from sfa.util.sfalogging import _SfaLogger from sfa.util.record import * from sfa.util.table import SfaTable @@ -52,8 +52,7 @@ def _cleanup_string(str): class sfaImport: def __init__(self): - sfa_logger_goes_to_import() - self.logger = sfa_logger() + self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog') self.AuthHierarchy = Hierarchy() self.config = Config() self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config)) diff --git a/sfa/rspecs/rspec_version.py b/sfa/rspecs/rspec_version.py index b96a765b..e6cdcec1 100755 --- a/sfa/rspecs/rspec_version.py +++ b/sfa/rspecs/rspec_version.py @@ -1,5 +1,5 @@ #!/usr/bin/python -from sfa.util.sfalogging import _SfaLogger +from sfa.util.sfalogging import logger class RSpecVersion(dict): @@ -11,7 +11,6 @@ class RSpecVersion(dict): } def __init__(self, version={}): - self.logger = _SfaLogger('/var/log/sfa.log') dict.__init__(self, self.fields) if not version: diff --git a/sfa/server/interface.py b/sfa/server/interface.py index a3b06ef6..12a0e4fe 100644 --- a/sfa/server/interface.py +++ b/sfa/server/interface.py @@ -1,7 +1,6 @@ import traceback import os.path -from sfa.util.sfalogging import sfa_logger from sfa.util.faults import * from sfa.util.storage import XmlStorage from sfa.util.xrn import get_authority, hrn_to_urn diff --git a/sfa/server/modpython/SfaAggregateModPython.py b/sfa/server/modpython/SfaAggregateModPython.py index bb61bb77..deaf89f9 100755 --- a/sfa/server/modpython/SfaAggregateModPython.py +++ b/sfa/server/modpython/SfaAggregateModPython.py @@ -13,7 +13,7 @@ import xmlrpclib from mod_python import apache from sfa.plc.api import SfaAPI -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger api = SfaAPI(interface='aggregate') @@ -53,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - sfa_logger().log_exc('%r'%err) + logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/modpython/SfaRegistryModPython.py b/sfa/server/modpython/SfaRegistryModPython.py index 0c460842..8879813a 100755 --- a/sfa/server/modpython/SfaRegistryModPython.py +++ b/sfa/server/modpython/SfaRegistryModPython.py @@ -13,7 +13,7 @@ import xmlrpclib from mod_python import apache from sfa.plc.api import SfaAPI -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger api = SfaAPI(interface='registry') @@ -53,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - sfa_logger().log_exc('%r'%err) + logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/modpython/SfaSliceMgrModPython.py b/sfa/server/modpython/SfaSliceMgrModPython.py index a28b002d..e0f2b923 100755 --- a/sfa/server/modpython/SfaSliceMgrModPython.py +++ b/sfa/server/modpython/SfaSliceMgrModPython.py @@ -13,7 +13,7 @@ import xmlrpclib from mod_python import apache from sfa.plc.api import SfaAPI -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger api = SfaAPI(interface='slicemgr') @@ -53,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - sfa_logger().log_exc('%r'%err) + logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/sfa-server.py b/sfa/server/sfa-server.py index 984b41c0..c981158b 100755 --- a/sfa/server/sfa-server.py +++ b/sfa/server/sfa-server.py @@ -37,7 +37,7 @@ import traceback import sys from optparse import OptionParser -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.trust.trustedroot import TrustedRootList from sfa.trust.certificate import Keypair, Certificate from sfa.trust.hierarchy import Hierarchy @@ -47,6 +47,7 @@ from sfa.plc.api import SfaAPI from sfa.server.registry import Registries from sfa.server.aggregate import Aggregates + # after http://www.erlenstar.demon.co.uk/unix/faq_2.html def daemon(): """Daemonize the current process.""" @@ -83,8 +84,9 @@ def init_server_key(server_key_file, server_cert_file, config, hierarchy): if not os.path.exists(key_file): # if it doesnt exist then this is probably a fresh interface # with no records. Generate a random keypair for now - sfa_logger().debug("server's public key not found in %s" % key_file) - sfa_logger().debug("generating a random server key pair") + logger.debug("server's public key not found in %s" % key_file) + + logger.debug("generating a random server key pair") key = Keypair(create=True) key.save_to_file(server_key_file) init_server_cert(hrn, key, server_cert_file, self_signed=True) @@ -113,18 +115,18 @@ def init_server_cert(hrn, key, server_cert_file, self_signed=False): else: try: # look for gid file - sfa_logger().debug("generating server cert from gid: %s"% hrn) + logger.debug("generating server cert from gid: %s"% hrn) hierarchy = Hierarchy() auth_info = hierarchy.get_auth_info(hrn) gid = GID(filename=auth_info.gid_filename) gid.save_to_file(filename=server_cert_file) except: # fall back to self signed cert - sfa_logger().debug("gid for %s not found" % hrn) + logger.debug("gid for %s not found" % hrn) init_self_signed_cert(hrn, key, server_cert_file) def init_self_signed_cert(hrn, key, server_cert_file): - sfa_logger().debug("generating self signed cert") + logger.debug("generating self signed cert") # generate self signed certificate cert = Certificate(subject=hrn) cert.set_issuer(key=key, subject=hrn) @@ -187,10 +189,9 @@ def main(): parser.add_option("-d", "--daemon", dest="daemon", action="store_true", help="Run as daemon.", default=False) (options, args) = parser.parse_args() - sfa_logger().setLevelFromOptVerbose(options.verbose) config = Config() - if config.SFA_API_DEBUG: sfa_logger().setLevelDebug() + if config.SFA_API_DEBUG: pass hierarchy = Hierarchy() server_key_file = os.path.join(hierarchy.basedir, "server.key") server_cert_file = os.path.join(hierarchy.basedir, "server.cert") @@ -227,4 +228,4 @@ if __name__ == "__main__": try: main() except: - sfa_logger().log_exc_critical("SFA server is exiting") + logger.log_exc_critical("SFA server is exiting") diff --git a/sfa/trust/auth.py b/sfa/trust/auth.py index 218783e2..54661896 100644 --- a/sfa/trust/auth.py +++ b/sfa/trust/auth.py @@ -12,7 +12,7 @@ from sfa.util.config import * from sfa.util.xrn import get_authority from sfa.util.sfaticket import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger class Auth: """ @@ -36,14 +36,14 @@ class Auth: valid = [] if not isinstance(creds, list): creds = [creds] - sfa_logger().debug("Auth.checkCredentials with %d creds"%len(creds)) + logger.debug("Auth.checkCredentials with %d creds"%len(creds)) for cred in creds: try: self.check(cred, operation, hrn) valid.append(cred) except: cred_obj=Credential(string=cred) - sfa_logger().debug("failed to validate credential - dump=%s"%cred_obj.dump_string(dump_parents=True)) + logger.debug("failed to validate credential - dump=%s"%cred_obj.dump_string(dump_parents=True)) error = sys.exc_info()[:2] continue diff --git a/sfa/trust/certificate.py b/sfa/trust/certificate.py index 9a2e862a..959b7638 100644 --- a/sfa/trust/certificate.py +++ b/sfa/trust/certificate.py @@ -46,9 +46,10 @@ from OpenSSL import crypto import M2Crypto from M2Crypto import X509 -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.util.xrn import urn_to_hrn from sfa.util.faults import * +from sfa.util.sfalogging import logger glo_passphrase_callback = None @@ -111,7 +112,7 @@ def convert_public_key(key): try: k.load_pubkey_from_file(ssl_fn) except: - sfa_logger().log_exc("convert_public_key caught exception") + logger.log_exc("convert_public_key caught exception") k = None # remove the temporary files @@ -330,7 +331,6 @@ class Certificate: if intermediate: self.set_intermediate_ca(intermediate) - ## # Create a blank X509 certificate and store it in this object. def create(self): @@ -564,7 +564,7 @@ class Certificate: # Sign the certificate using the issuer private key and issuer subject previous set with set_issuer(). def sign(self): - sfa_logger().debug('certificate.sign') + logger.debug('certificate.sign') assert self.cert != None assert self.issuerSubject != None assert self.issuerKey != None @@ -649,7 +649,7 @@ class Certificate: # verify expiration time if self.cert.has_expired(): - sfa_logger().debug("verify_chain: NO our certificate has expired") + logger.debug("verify_chain: NO our certificate has expired") raise CertExpired(self.get_subject(), "client cert") # if this cert is signed by a trusted_cert, then we are set @@ -657,26 +657,26 @@ class Certificate: if self.is_signed_by_cert(trusted_cert): # verify expiration of trusted_cert ? if not trusted_cert.cert.has_expired(): - sfa_logger().debug("verify_chain: YES cert %s signed by trusted cert %s"%( + logger.debug("verify_chain: YES cert %s signed by trusted cert %s"%( self.get_subject(), trusted_cert.get_subject())) return trusted_cert else: - sfa_logger().debug("verify_chain: NO cert %s is signed by trusted_cert %s, but this is expired..."%( + logger.debug("verify_chain: NO cert %s is signed by trusted_cert %s, but this is expired..."%( self.get_subject(),trusted_cert.get_subject())) raise CertExpired(self.get_subject(),"trusted_cert %s"%trusted_cert.get_subject()) # if there is no parent, then no way to verify the chain if not self.parent: - sfa_logger().debug("verify_chain: NO %s has no parent and is not in trusted roots"%self.get_subject()) + logger.debug("verify_chain: NO %s has no parent and is not in trusted roots"%self.get_subject()) raise CertMissingParent(self.get_subject()) # if it wasn't signed by the parent... if not self.is_signed_by_cert(self.parent): - sfa_logger().debug("verify_chain: NO %s is not signed by parent"%self.get_subject()) + logger.debug("verify_chain: NO %s is not signed by parent"%self.get_subject()) return CertNotSignedByParent(self.get_subject()) # if the parent isn't verified... - sfa_logger().debug("verify_chain: .. %s, -> verifying parent %s"%(self.get_subject(),self.parent.get_subject())) + logger.debug("verify_chain: .. %s, -> verifying parent %s"%(self.get_subject(),self.parent.get_subject())) self.parent.verify_chain(trusted_certs) return @@ -687,7 +687,7 @@ class Certificate: triples=[] m2x509 = X509.load_cert_string(self.save_to_string()) nb_extensions=m2x509.get_ext_count() - sfa_logger().debug("X509 had %d extensions"%nb_extensions) + logger.debug("X509 had %d extensions"%nb_extensions) for i in range(nb_extensions): ext=m2x509.get_ext_at(i) triples.append( (ext.get_name(), ext.get_value(), ext.get_critical(),) ) diff --git a/sfa/trust/credential.py b/sfa/trust/credential.py index 2f38c38a..3e1fbcc6 100644 --- a/sfa/trust/credential.py +++ b/sfa/trust/credential.py @@ -35,7 +35,7 @@ from xml.dom.minidom import Document, parseString from lxml import etree from sfa.util.faults import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.trust.certificate import Keypair from sfa.trust.credential_legacy import CredentialLegacy from sfa.trust.rights import Right, Rights @@ -675,7 +675,7 @@ class Credential(object): trusted_cert_objects.append(GID(filename=f)) ok_trusted_certs.append(f) except Exception, exc: - sfa_logger().error("Failed to load trusted cert from %s: %r"%( f, exc)) + logger.error("Failed to load trusted cert from %s: %r"%( f, exc)) trusted_certs = ok_trusted_certs # Use legacy verification if this is a legacy credential @@ -759,7 +759,7 @@ class Credential(object): # Maybe should be (hrn, type) = urn_to_hrn(root_cred_signer.get_urn()) root_cred_signer_type = root_cred_signer.get_type() if (root_cred_signer_type == 'authority'): - #sfa_logger().debug('Cred signer is an authority') + #logger.debug('Cred signer is an authority') # signer is an authority, see if target is in authority's domain hrn = root_cred_signer.get_hrn() if root_target_gid.get_hrn().startswith(hrn): diff --git a/sfa/trust/gid.py b/sfa/trust/gid.py index 94240cdd..7650d11f 100644 --- a/sfa/trust/gid.py +++ b/sfa/trust/gid.py @@ -28,7 +28,7 @@ import xmlrpclib import uuid -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.trust.certificate import Certificate from sfa.util.xrn import hrn_to_urn, urn_to_hrn @@ -80,7 +80,7 @@ class GID(Certificate): Certificate.__init__(self, create, subject, string, filename) if subject: - sfa_logger().debug("Creating GID for subject: %s" % subject) + logger.debug("Creating GID for subject: %s" % subject) if uuid: self.uuid = int(uuid) if hrn: diff --git a/sfa/trust/hierarchy.py b/sfa/trust/hierarchy.py index 5d7db6f6..f28329ce 100644 --- a/sfa/trust/hierarchy.py +++ b/sfa/trust/hierarchy.py @@ -15,7 +15,7 @@ import os from sfa.util.faults import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.util.xrn import get_leaf, get_authority, hrn_to_urn, urn_to_hrn from sfa.trust.certificate import Keypair from sfa.trust.credential import Credential @@ -33,7 +33,6 @@ class AuthInfo: gid_filename = None privkey_filename = None dbinfo_filename = None - ## # Initialize and authority object. # @@ -159,7 +158,7 @@ class Hierarchy: def create_auth(self, xrn, create_parents=False): hrn, type = urn_to_hrn(xrn) - sfa_logger().debug("Hierarchy: creating authority: %s"% hrn) + logger.debug("Hierarchy: creating authority: %s"% hrn) # create the parent authority if necessary parent_hrn = get_authority(hrn) @@ -179,7 +178,7 @@ class Hierarchy: pass if os.path.exists(privkey_filename): - sfa_logger().debug("using existing key %r for authority %r"%(privkey_filename,hrn)) + logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn)) pkey = Keypair(filename = privkey_filename) else: pkey = Keypair(create = True) @@ -205,7 +204,7 @@ class Hierarchy: def get_auth_info(self, xrn): hrn, type = urn_to_hrn(xrn) if not self.auth_exists(hrn): - sfa_logger().warning("Hierarchy: mising authority - xrn=%s, hrn=%s"%(xrn,hrn)) + logger.warning("Hierarchy: mising authority - xrn=%s, hrn=%s"%(xrn,hrn)) raise MissingAuthority(hrn) (directory, gid_filename, privkey_filename, dbinfo_filename) = \ diff --git a/sfa/util/PostgreSQL.py b/sfa/util/PostgreSQL.py index 39cde57b..19cd4d0e 100644 --- a/sfa/util/PostgreSQL.py +++ b/sfa/util/PostgreSQL.py @@ -22,7 +22,7 @@ try: import pgdb except: print >> sys.stderr, "WARNING, could not import pgdb" from sfa.util.faults import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger if not psycopg2: is8bit = re.compile("[\x80-\xff]").search @@ -183,21 +183,21 @@ class PostgreSQL: if not params: if self.debug: - sfa_logger().debug('execute0 %r'%query) + logger.debug('execute0 %r'%query) cursor.execute(query) elif isinstance(params,dict): if self.debug: - sfa_logger().debug('execute-dict: params=[%r] query=[%r]'%(params,query%params)) + logger.debug('execute-dict: params=[%r] query=[%r]'%(params,query%params)) cursor.execute(query,params) elif isinstance(params,tuple) and len(params)==1: if self.debug: - sfa_logger().debug('execute-tuple %r'%(query%params[0])) + logger.debug('execute-tuple %r'%(query%params[0])) cursor.execute(query,params[0]) else: param_seq=(params,) if self.debug: for params in param_seq: - sfa_logger().debug('executemany %r'%(query%params)) + logger.debug('executemany %r'%(query%params)) cursor.executemany(query, param_seq) (self.rowcount, self.description, self.lastrowid) = \ (cursor.rowcount, cursor.description, cursor.lastrowid) @@ -207,11 +207,11 @@ class PostgreSQL: except: pass uuid = commands.getoutput("uuidgen") - sfa_logger().error("Database error %s:" % uuid) - sfa_logger().error("Exception=%r"%e) - sfa_logger().error("Query=%r"%query) - sfa_logger().error("Params=%r"%pformat(params)) - sfa_logger().log_exc("PostgreSQL.execute caught exception") + logger.error("Database error %s:" % uuid) + logger.error("Exception=%r"%e) + logger.error("Query=%r"%query) + logger.error("Params=%r"%pformat(params)) + logger.log_exc("PostgreSQL.execute caught exception") raise SfaDBError("Please contact support: %s" % str(e)) return cursor diff --git a/sfa/util/api.py b/sfa/util/api.py index f9aed1cf..67d155e7 100644 --- a/sfa/util/api.py +++ b/sfa/util/api.py @@ -8,7 +8,7 @@ import traceback import string import xmlrpclib -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.trust.auth import Auth from sfa.util.config import * from sfa.util.faults import * @@ -143,8 +143,8 @@ class BaseAPI: self.credential = None self.source = None self.time_format = "%Y-%m-%d %H:%M:%S" - self.logger=sfa_logger - + self.logger = logger + # load registries from sfa.server.registry import Registries self.registries = Registries(self) @@ -238,7 +238,7 @@ class BaseAPI: except SfaFault, fault: result = fault except Exception, fault: - sfa_logger().log_exc("BaseAPI.handle has caught Exception") + logger.log_exc("BaseAPI.handle has caught Exception") result = SfaAPIError(fault) diff --git a/sfa/util/callids.py b/sfa/util/callids.py index 904c42b4..ead60bbe 100644 --- a/sfa/util/callids.py +++ b/sfa/util/callids.py @@ -3,7 +3,7 @@ import threading import time -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger """ Callids: a simple mechanism to remember the call ids served so fas @@ -33,15 +33,15 @@ class _call_ids_impl (dict): if not call_id: return False has_lock=False for attempt in range(_call_ids_impl.retries): - if debug: sfa_logger().debug("Waiting for lock (%d)"%attempt) + if debug: logger.debug("Waiting for lock (%d)"%attempt) if self._lock.acquire(False): has_lock=True - if debug: sfa_logger().debug("got lock (%d)"%attempt) + if debug: logger.debug("got lock (%d)"%attempt) break time.sleep(float(_call_ids_impl.wait_ms)/1000) # in the unlikely event where we can't get the lock if not has_lock: - sfa_logger().warning("_call_ids_impl.should_handle_call_id: could not acquire lock") + logger.warning("_call_ids_impl.should_handle_call_id: could not acquire lock") return False # we're good to go if self.has_key(call_id): @@ -51,7 +51,7 @@ class _call_ids_impl (dict): self[call_id]=time.time() self._purge() self._lock.release() - if debug: sfa_logger().debug("released lock") + if debug: logger.debug("released lock") return False def _purge(self): @@ -60,11 +60,11 @@ class _call_ids_impl (dict): for (k,v) in self.iteritems(): if (now-v) >= _call_ids_impl.purge_timeout: o_keys.append(k) for k in o_keys: - if debug: sfa_logger().debug("Purging call_id %r (%s)"%(k,time.strftime("%H:%M:%S",time.localtime(self[k])))) + if debug: logger.debug("Purging call_id %r (%s)"%(k,time.strftime("%H:%M:%S",time.localtime(self[k])))) del self[k] if debug: - sfa_logger().debug("AFTER PURGE") - for (k,v) in self.iteritems(): sfa_logger().debug("%s -> %s"%(k,time.strftime("%H:%M:%S",time.localtime(v)))) + logger.debug("AFTER PURGE") + for (k,v) in self.iteritems(): logger.debug("%s -> %s"%(k,time.strftime("%H:%M:%S",time.localtime(v)))) def Callids (): if not _call_ids_impl._instance: diff --git a/sfa/util/componentserver.py b/sfa/util/componentserver.py index c12104b2..9fa40f97 100644 --- a/sfa/util/componentserver.py +++ b/sfa/util/componentserver.py @@ -19,7 +19,7 @@ import SimpleHTTPServer import SimpleXMLRPCServer from OpenSSL import SSL -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.trust.certificate import Keypair, Certificate from sfa.trust.credential import * from sfa.util.faults import * @@ -74,7 +74,7 @@ class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): # internal error, report as HTTP server error self.send_response(500) self.end_headers() - sfa_logger().log_exc("componentserver.SecureXMLRpcRequestHandler.do_POST") + logger.log_exc("componentserver.SecureXMLRpcRequestHandler.do_POST") else: # got a valid XML RPC response self.send_response(200) diff --git a/sfa/util/method.py b/sfa/util/method.py index 43b589c1..4c37c676 100644 --- a/sfa/util/method.py +++ b/sfa/util/method.py @@ -11,7 +11,7 @@ import textwrap import xmlrpclib -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger from sfa.util.faults import * from sfa.util.parameter import Parameter, Mixed, python_type, xmlrpc_type from sfa.trust.auth import Auth @@ -80,12 +80,12 @@ class Method: self.type_check(name, value, expected, args) if self.api.config.SFA_API_DEBUG: - sfa_logger().debug("method.__call__ [%s] : BEG %s"%(self.api.interface,methodname)) + logger.debug("method.__call__ [%s] : BEG %s"%(self.api.interface,methodname)) result = self.call(*args, **kwds) runtime = time.time() - start if self.api.config.SFA_API_DEBUG or hasattr(self, 'message'): - sfa_logger().debug("method.__call__ [%s] : END %s in %02f s (%s)"%\ + logger.debug("method.__call__ [%s] : END %s in %02f s (%s)"%\ (self.api.interface,methodname,runtime,getattr(self,'message',"[no-msg]"))) return result @@ -97,7 +97,7 @@ class Method: # Prepend caller and method name to expected faults fault.faultString = caller + ": " + self.name + ": " + fault.faultString runtime = time.time() - start - sfa_logger().log_exc("Method %s raised an exception"%self.name) + logger.log_exc("Method %s raised an exception"%self.name) raise fault diff --git a/sfa/util/rspec.py b/sfa/util/rspec.py index ffc816c3..572d6600 100644 --- a/sfa/util/rspec.py +++ b/sfa/util/rspec.py @@ -7,7 +7,7 @@ import httplib from xml.dom import minidom from lxml import etree -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import info_logger class RSpec: @@ -232,11 +232,11 @@ class RSpec: except Exception, e: # logging.debug("%s: web file not found" % xsdURI) # logging.debug("Using local file %s" % self.xsd") - sfa_logger().log_exc("rspec.parseXSD: can't find %s on the web. Continuing." % xsdURI) + info_logger.log_exc("rspec.parseXSD: can't find %s on the web. Continuing." % xsdURI) if not schemaDom: if os.path.exists(xsdURI): # logging.debug("using local copy.") - sfa_logger().debug("rspec.parseXSD: Using local %s" % xsdURI) + info_logger.debug("rspec.parseXSD: Using local %s" % xsdURI) schemaDom = minidom.parse(xsdURI) else: raise Exception("rspec.parseXSD: can't find xsd locally") diff --git a/sfa/util/rspecHelper.py b/sfa/util/rspecHelper.py index 1ccc9848..89f15afd 100755 --- a/sfa/util/rspecHelper.py +++ b/sfa/util/rspecHelper.py @@ -8,7 +8,7 @@ from StringIO import StringIO from optparse import OptionParser from sfa.util.faults import * -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger def merge_rspecs(rspecs): """ @@ -24,13 +24,13 @@ def merge_rspecs(rspecs): try: known_networks[network.get('name')]=True except: - sfa_logger().error("merge_rspecs: cannot register network with no name in rspec") + logger.error("merge_rspecs: cannot register network with no name in rspec") pass def is_registered_network (network): try: return network.get('name') in known_networks except: - sfa_logger().error("merge_rspecs: cannot retrieve network with no name in rspec") + logger.error("merge_rspecs: cannot retrieve network with no name in rspec") return False # the resulting tree @@ -42,13 +42,13 @@ def merge_rspecs(rspecs): tree = etree.parse(StringIO(input_rspec)) except etree.XMLSyntaxError: # consider failing silently here - sfa_logger().log_exc("merge_rspecs, parse error") + logger.log_exc("merge_rspecs, parse error") message = str(sys.exc_info()[1]) + ' with ' + input_rspec raise InvalidRSpec(message) root = tree.getroot() if not root.get("type") in ["SFA"]: - sfa_logger().error("merge_rspecs: unexpected type for rspec root, %s"%root.get('type')) + logger.error("merge_rspecs: unexpected type for rspec root, %s"%root.get('type')) continue if rspec == None: # we scan the first input, register all networks diff --git a/sfa/util/server.py b/sfa/util/server.py index b4fd2ffa..fadd4cad 100644 --- a/sfa/util/server.py +++ b/sfa/util/server.py @@ -24,7 +24,7 @@ from sfa.trust.credential import * from sfa.util.faults import * from sfa.plc.api import SfaAPI from sfa.util.cache import Cache -from sfa.util.sfalogging import sfa_logger +from sfa.util.sfalogging import logger ## # Verification callback for pyOpenSSL. We do our own checking of keys because @@ -110,7 +110,7 @@ class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): except Exception, fault: # This should only happen if the module is buggy # internal error, report as HTTP server error - sfa_logger().log_exc("server.do_POST") + logger.log_exc("server.do_POST") response = self.api.prepare_response(fault) #self.send_response(500) #self.end_headers() @@ -134,7 +134,7 @@ class SecureXMLRPCServer(BaseHTTPServer.HTTPServer,SimpleXMLRPCServer.SimpleXMLR It it very similar to SimpleXMLRPCServer but it uses HTTPS for transporting XML data. """ - sfa_logger().debug("SecureXMLRPCServer.__init__, server_address=%s, cert_file=%s"%(server_address,cert_file)) + logger.debug("SecureXMLRPCServer.__init__, server_address=%s, cert_file=%s"%(server_address,cert_file)) self.logRequests = logRequests self.interface = None self.key_file = key_file @@ -171,7 +171,7 @@ class SecureXMLRPCServer(BaseHTTPServer.HTTPServer,SimpleXMLRPCServer.SimpleXMLR # the client. def _dispatch(self, method, params): - sfa_logger().debug("SecureXMLRPCServer._dispatch, method=%s"%method) + logger.debug("SecureXMLRPCServer._dispatch, method=%s"%method) try: return SimpleXMLRPCServer.SimpleXMLRPCDispatcher._dispatch(self, method, params) except: @@ -287,7 +287,7 @@ class SfaServer(threading.Thread): self.server.interface=interface self.trusted_cert_list = None self.register_functions() - sfa_logger().info("Starting SfaServer, interface=%s"%interface) + logger.info("Starting SfaServer, interface=%s"%interface) ## # Register functions that will be served by the XMLRPC server. This diff --git a/sfa/util/sfalogging.py b/sfa/util/sfalogging.py index b4a12905..f812517c 100755 --- a/sfa/util/sfalogging.py +++ b/sfa/util/sfalogging.py @@ -15,21 +15,21 @@ 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() - handler.setFormatter(logging.Formatter("%(levelname)s %(message)s")) - 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) - handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) - + #loggername='console' + #handler=logging.StreamHandler() + #handler.setFormatter(logging.Formatter("%(levelname)s %(message)s")) + logfile = "/var/log/sfa.log" + + 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) + handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) self.logger=logging.getLogger(loggername) self.logger.setLevel(level) self.logger.addHandler(handler) @@ -84,30 +84,13 @@ class _SfaLogger: 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 _server_logger + +info_logger = _SfaLogger(loggername='info', level=logging.INFO) +debug_logger = _SfaLogger(loggername='debug', level=logging.DEBUG) +warn_logger = _SfaLogger(loggername='warning', level=logging.WARNING) +error_logger = _SfaLogger(loggername='error', level=logging.ERROR) +critical_logger = _SfaLogger(loggername='critical', level=logging.CRITICAL) +logger = info_logger ######################################## import time @@ -145,9 +128,6 @@ if __name__ == '__main__': 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): diff --git a/sfa/util/xmlrpcprotocol.py b/sfa/util/xmlrpcprotocol.py index 61e16fe3..b5e9c01f 100644 --- a/sfa/util/xmlrpcprotocol.py +++ b/sfa/util/xmlrpcprotocol.py @@ -3,8 +3,7 @@ import httplib import xmlrpclib -from sfa.util.sfalogging import sfa_logger - +from sfa.util.sfalogging import logger ## # ServerException, ExceptionUnmarshaller # @@ -58,11 +57,10 @@ class XMLRPCServerProxy(xmlrpclib.ServerProxy): verbose = False if options and options.debug: verbose = True -# sfa_logger().debug ("xmlrpcprotocol.XMLRPCServerProxy.__init__ %s (with verbose=%s)"%(url,verbose)) xmlrpclib.ServerProxy.__init__(self, url, transport, allow_none=allow_none, verbose=verbose) def __getattr__(self, attr): - sfa_logger().debug ("xml-rpc %s method:%s"%(self.url,attr)) + logger.debug ("xml-rpc %s method:%s"%(self.url,attr)) return xmlrpclib.ServerProxy.__getattr__(self, attr) diff --git a/sfa/util/xrn.py b/sfa/util/xrn.py index d5ad2d45..752e7a9e 100644 --- a/sfa/util/xrn.py +++ b/sfa/util/xrn.py @@ -1,7 +1,6 @@ import re from sfa.util.faults import * -from sfa.util.sfalogging import sfa_logger # for convenience and smoother translation - we should get rid of these functions eventually def get_leaf(hrn): return Xrn(hrn).get_leaf() @@ -74,7 +73,7 @@ class Xrn: self.hrn_to_urn() # happens all the time .. # if not type: -# sfa_logger().debug("type-less Xrn's are not safe") +# debug_logger.debug("type-less Xrn's are not safe") def get_urn(self): return self.urn def get_hrn(self): return self.hrn -- 2.43.0