# 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')
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)
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)
# IMPORTS
import configparser
+import logging
import sys
from configparser import Error
* 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')
# 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.