# entry per line
dev-seedfile: /etc/gentoo-keys/developer.seeds
+
+# logfile directory
+logdir: %(keysdir)s/logs
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 = {
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)
@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)
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))
def _action_updateseeds(self, args):
+ logger.info("Beginning ldap search...")
print("Beginning ldap search...")
l = LdapSearch()
if not l.connect():
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
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)
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):
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)
@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)
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
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)
"""
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
+
+