]> kaliko git repositories - mpd-sima.git/commitdiff
Improved log handling
authorkaliko <kaliko@azylum.org>
Sun, 15 Jun 2014 23:10:03 +0000 (01:10 +0200)
committerkaliko <kaliko@azylum.org>
Sun, 15 Jun 2014 23:10:03 +0000 (01:10 +0200)
sima/launch.py
sima/lib/logger.py
sima/utils/config.py

index 19fac0c3c06979283acda38ead04dc3b3ddfacf7..733d237e2f12b3f0714c9c2ae2214aac6b977963 100644 (file)
@@ -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')
index 3b287e26390859d47c2b11b7af7dedfecca88f81..05f9c35011e120ed07077077dd0c42d45ac22b9e 100644 (file)
@@ -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)
 
index e9da8f8f4c245f72b38a27f58f4e97df654c0002..b3a8d46a8c0c9a4d94ed001295226e631eecf61e 100644 (file)
@@ -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.