From: kaliko Date: Sun, 15 Jun 2014 23:10:03 +0000 (+0200) Subject: Improved log handling X-Git-Tag: 0.12.0~2 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=325e87e6aaf33974ea8bc8e4b3d2fdbbef56e9a9;p=mpd-sima.git Improved log handling --- diff --git a/sima/launch.py b/sima/launch.py index 19fac0c..733d237 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -79,15 +79,13 @@ def start(sopt, restart=False): # set logger verbosity = sopt.options.get('verbosity', 'warning') logfile = sopt.options.get('logfile', None) - cli_loglevel = getattr(logging, verbosity.upper()) - set_logger(level=verbosity, logfile=logfile) - logger = logging.getLogger('sima') - logger.setLevel(cli_loglevel) + set_logger(verbosity, logfile) # loads configuration - config = ConfMan(logger, sopt.options).config - logger.setLevel(getattr(logging, - config.get('log', 'verbosity').upper())) # pylint: disable=E1103 - + config = ConfMan(sopt.options).config + logfile = config.get('log', 'logfile') + verbosity = config.get('log', 'verbosity') + set_logger(verbosity, logfile) + logger = logging.getLogger('sima') logger.debug('Command line say: {0}'.format(sopt.options)) # Create Database db_file = config.get('sima', 'db_file') diff --git a/sima/lib/logger.py b/sima/lib/logger.py index 3b287e2..05f9c35 100644 --- a/sima/lib/logger.py +++ b/sima/lib/logger.py @@ -50,13 +50,14 @@ class LevelFilter(logging.Filter):# Logging facility return record.levelno <= self.level -def set_logger(level='info', logfile=None, name='sima'): +def set_logger(level='info', logfile=None): """ logger: level: in debug, info, warning,… - file: provides to log to file + logfile: file to log to """ + name = 'sima' user_log_level = getattr(logging, level.upper()) if user_log_level > logging.DEBUG: log_format = LOG_FORMATS.get(logging.INFO) @@ -65,22 +66,34 @@ def set_logger(level='info', logfile=None, name='sima'): logg = logging.getLogger(name) formatter = logging.Formatter(log_format, DATE_FMT, '{') logg.setLevel(user_log_level) + filehdl = False + if logg.handlers: + for hdl in logg.handlers: + hdl.setFormatter(formatter) + if isinstance(hdl, logging.FileHandler): + filehdl = True + else: + logg.removeHandler(hdl) + if logfile: + if filehdl: + logg.handlers = [] # create file handler fileh = logging.FileHandler(logfile) #fileh.setLevel(user_log_level) fileh.setFormatter(formatter) - if not logg.hasHandlers(): - logg.addHandler(fileh) + logg.addHandler(fileh) else: - if not logg.hasHandlers(): - # create console handler with a specified log level (STDOUT) - couth = logging.StreamHandler(sys.stdout) - #couth.setLevel(user_log_level) - couth.addFilter(LevelFilter(logging.WARNING)) - - # create console handler with warning log level (STDERR) - cerrh = logging.StreamHandler(sys.stderr) + if filehdl: + logg.info('Not changing logging handlers, only updating formatter') + return + # create console handler with a specified log level (STDOUT) + couth = logging.StreamHandler(sys.stdout) + #couth.setLevel(user_log_level) + couth.addFilter(LevelFilter(logging.WARNING)) + + # create console handler with warning log level (STDERR) + cerrh = logging.StreamHandler(sys.stderr) #cerrh.setLevel(logging.WARNING) cerrh.setLevel(logging.ERROR) diff --git a/sima/utils/config.py b/sima/utils/config.py index e9da8f8..b3a8d46 100644 --- a/sima/utils/config.py +++ b/sima/utils/config.py @@ -25,6 +25,7 @@ Parse configuration file and set defaults for missing options. # IMPORTS import configparser +import logging import sys from configparser import Error @@ -105,8 +106,8 @@ class ConfMan(object): # CONFIG MANAGER CLASS * command line options (overrides previous) """ - def __init__(self, logger, options=None): - self.log = logger + def __init__(self, options=None): + self.log = logging.getLogger('sima') # options settings priority: # defauts < env. var. < conf. file < command line self.conf_file = options.get('conf_file') @@ -122,20 +123,6 @@ class ConfMan(object): # CONFIG MANAGER CLASS # generate dbfile self.config['sima']['db_file'] = join(self.config['sima']['var_dir'], 'sima.db') - def get_pw(self): - try: - self.config.getboolean('MPD', 'password') - self.log.debug('No password set, proceeding without ' + - 'authentication...') - return None - except ValueError: - # ValueError if password not a boolean, hence an actual password. - pwd = self.config.get('MPD', 'password') - if not pwd: - self.log.debug('Password set as an empty string.') - return None - return pwd - def control_mod(self): """ Controls conf file permissions.