From c363d40351aa7e21455694dbbf182d9b964f3361 Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Fri, 28 Jun 2013 12:17:10 -0700 Subject: [PATCH] Initial logfile capability Add logfile capability to gkeyldap Remove logger.setLevel() Create logfiles and set different levels for the terminal and logfile. --- etc/gkeys.conf | 3 +++ gkeyldap/cli.py | 46 ++++++++++++++++++++++++++++------------ gkeyldap/search.py | 9 +++----- gkeys/cli.py | 36 +++++++++++++++++++++---------- gkeys/config.py | 13 +++++++----- gkeys/log.py | 53 ++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 120 insertions(+), 40 deletions(-) diff --git a/etc/gkeys.conf b/etc/gkeys.conf index acec4e8..5b91597 100644 --- a/etc/gkeys.conf +++ b/etc/gkeys.conf @@ -36,3 +36,6 @@ release-seedfile: /etc/gentoo-keys/release.seeds # entry per line dev-seedfile: /etc/gentoo-keys/developer.seeds + +# logfile directory +logdir: %(keysdir)s/logs diff --git a/gkeyldap/cli.py b/gkeyldap/cli.py index 6a3e4bb..32f1f87 100644 --- a/gkeyldap/cli.py +++ b/gkeyldap/cli.py @@ -9,17 +9,17 @@ import os import argparse from gkeys import log -log.set_logger('gkeyldap') -from gkeys.log import logger +from gkeys.log import log_levels, set_logger + +from gkeys import config +from gkeys import seed from gkeys.config import GKeysConfig, GKEY from gkeys.seed import Seeds +from gkeyldap import search from gkeyldap.search import (LdapSearch, UID, gkey2ldap_map, gkey2SEARCH) - -# set debug level to min -logger.setLevel(0) - +logger = log.logger # set some defaults KEY_LEN = { @@ -103,8 +103,9 @@ class Main(object): help='The gpg fingerprint to search for') parser.add_argument('-S', '--status', default=False, help='The seedfile path to use') - parser.add_argument('-D', '--debug', default=0, - help='The logging level to use and report with') + parser.add_argument('-D', '--debug', default='DEBUG', + choices=list(log_levels), + help='The logging level to set for the logfile') return parser.parse_args(args) @@ -114,20 +115,33 @@ class Main(object): @param args: list or argparse.Namespace object ''' + global logger + message = None if not args: - logger.error("Main: run; invalid args argument passed in") + message = "Main: run; invalid args argument passed in" if isinstance(args, list): args = self.parse_args(args) - if args.debug: - logger.setLevel(int(args.debug)) - logger.debug("MAIN: run; Found alternate debug setting: %s" % str(args.debug)) if args.config: - logger.debug("Main: run; Found alternate config request: %s" - % args.config) self.config.defaults['config'] = args.config # now make it load the config file self.config.read_config() + # establish our logger and update it in the imported files + logger = set_logger('gkeyldap', self.config['logdir'], args.debug) + config.logger = logger + seed.logger = logger + search.logger = logger + + if message: + logger.error(message) + + # now that we have a logger, record the alternate config setting + if args.config: + logger.debug("Main: run; Found alternate config request: %s" + % args.config) + + logger.info("Begin running action: %s" % args.action) + func = getattr(self, '_action_%s' % args.action) logger.debug('Main: run; Found action: %s' % args.action) results = func(args) @@ -136,7 +150,10 @@ class Main(object): def _action_ldapsearch(self, args): l = LdapSearch() + logger.info("Search...establishing connection") + print("Search...establishing connection") if not l.connect(): + logger.info("Aborting Search...Connection failed") print("Aborting Search...Connection failed") return False logger.debug("MAIN: _action_ldapsearch; args = %s" % str(args)) @@ -151,6 +168,7 @@ class Main(object): def _action_updateseeds(self, args): + logger.info("Beginning ldap search...") print("Beginning ldap search...") l = LdapSearch() if not l.connect(): diff --git a/gkeyldap/search.py b/gkeyldap/search.py index 169124f..f7d2045 100644 --- a/gkeyldap/search.py +++ b/gkeyldap/search.py @@ -4,13 +4,10 @@ import ldap -from gkeys.log import logger +from gkeys import log from gkeys.config import GKEY - -# set debug level to max -logger.setLevel(1) - +logger = log.logger default_server = 'ldap://ldap1.gentoo.org' # add uid to the results so you don't have to @@ -79,7 +76,7 @@ class LdapSearch(object): self.ldap_connection.start_tls_s() self.ldap_connection.simple_bind_s() except Exception as e: - logger.error('LdapSearch: connect; failed to connect ot server: %s' % self.server) + logger.error('LdapSearch: connect; failed to connect to server: %s' % self.server) logger.error("Exception was: %s" % str(e)) return False logger.debug('LdapSearch: connect; connection: %s' % self.ldap_connection) diff --git a/gkeys/cli.py b/gkeys/cli.py index 3c9bc49..2522531 100644 --- a/gkeys/cli.py +++ b/gkeys/cli.py @@ -17,16 +17,17 @@ import argparse import sys from gkeys import log -log.set_logger('gkeys') -from gkeys.log import logger +from gkeys.log import log_levels, set_logger + +from gkeys import config +from gkeys import seed +from gkeys import lib from gkeys.config import GKeysConfig, GKEY from gkeys.seed import Seeds from gkeys.lib import GkeysGPG -# set debug level to min -logger.setLevel(0) class Main(object): @@ -92,8 +93,9 @@ class Main(object): help='The seeds file to use or update') parser.add_argument('-S', '--seedfile', dest='seedfile', default=None, help='The seedfile path to use') - parser.add_argument('-D', '--debug', default=0, - help='The logging level to use and report with') + parser.add_argument('-D', '--debug', default='DEBUG', + choices=list(log_levels), + help='The logging level to set for the logfile') return parser.parse_args(args) @@ -103,19 +105,31 @@ class Main(object): @param args: list or argparse.Namespace object ''' + global logger + message = None if not args: - logger.error("Main: run; invalid args argument passed in") + message = "Main: run; invalid args argument passed in" if isinstance(args, list): args = self.parse_args(args) - if args.debug: - logger.setLevel(int(args.debug)) - logger.debug("MAIN: run; Found alternate debug setting: %s" % str(args.debug)) if args.config: - logger.debug("Main: run; Found alternate config request: %s" % args.config) self.config.defaults['config'] = args.config # now make it load the config file self.config.read_config() + # establish our logger and update it in the imported files + logger = set_logger('gkeys', self.config['logdir'], args.debug) + config.logger = logger + seed.logger = logger + lib.logger = logger + + if message: + logger.error(message) + + # now that we have a logger, record the alternate config setting + if args.config: + logger.debug("Main: run; Found alternate config request: %s" + % args.config) + # run the action func = getattr(self, '_action_%s' % args.action) logger.debug('Main: run; Found action: %s' % args.action) diff --git a/gkeys/config.py b/gkeys/config.py index e78f487..50d43c6 100644 --- a/gkeys/config.py +++ b/gkeys/config.py @@ -17,9 +17,11 @@ from collections import namedtuple from pygpg.config import GPGConfig -from gkeys.log import logger +from gkeys import log from gkeys.utils import path +logger = log.logger + # establish the eprefix, initially set so eprefixify can # set it on install @@ -84,10 +86,11 @@ class GKeysConfig(GPGConfig): def _get_(self, key, subkey=None): if self.configparser and self.configparser.has_option('MAIN', key): - logger.debug("Found %s in configparser... %s" - % (key, str(self.configparser.get('MAIN', key)))) - logger.debug("type(key)= %s" - % str(type(self.configparser.get('MAIN', key)))) + if logger: + logger.debug("Found %s in configparser... %s" + % (key, str(self.configparser.get('MAIN', key)))) + #logger.debug("type(key)= %s" + # % str(type(self.configparser.get('MAIN', key)))) return self.configparser.get('MAIN', key) else: return super(GKeysConfig, self)._get_(key, subkey) diff --git a/gkeys/log.py b/gkeys/log.py index 4608eba..6aa8711 100644 --- a/gkeys/log.py +++ b/gkeys/log.py @@ -11,18 +11,63 @@ """ import logging +import time +import os -logging.basicConfig() NAMESPACE = 'gentoo-keys' logger = None +Console_handler = None +File_handler = None -def set_logger(namespace=None): - global logger, NAMESPACE +log_levels = { + 'CRITICAL': logging.CRITICAL, + 'DEBUG': logging.DEBUG, + 'ERROR': logging.ERROR, + 'FATAL': logging.FATAL, + 'INFO': logging.INFO, + 'NOTSET': logging.NOTSET, + 'WARN': logging.WARN, + 'WARNING':logging.WARNING, +} + + + +def set_logger(namespace=None, logpath='', level=None): + global logger, NAMESPACE, Console_handler, File_handler if not namespace: - namespace = Namespace + namespace = NAMESPACE else: NAMESPACE = namespace logger = logging.getLogger(namespace) + logger.setLevel(log_levels['DEBUG']) + # create formatter and add it to the handlers + log_format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' + formatter = logging.Formatter(log_format) + # add the handlers to logger + if logpath: + logname = os.path.join(logpath, + '%s-%s.log' % (namespace,time.strftime('%Y%m%d-%H:%M'))) + File_handler = logging.FileHandler(logname) + if level: + #print "Setting cli log level", level, log_levels[level] + File_handler.setLevel(log_levels[level]) + else: + #print "Create file handler which logs even debug messages" + File_handler.setLevel(log_levels['DEBUG']) + + File_handler.setFormatter(formatter) + # create console handler with a higher log level + Console_handler = logging.StreamHandler() + Console_handler.setLevel(logging.ERROR) + #Console_handler.setFormatter(formatter) + logger.addHandler(Console_handler) + logger.addHandler(File_handler) + #print "File logger suppose to be initialized", logger, File_handler, Console_handler + logger.debug("Loggers initialized") + + return logger + + -- 2.26.2