From 1b0d4cdcf9d82572cb7fac7e53513316196a8d39 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Thu, 19 Jun 2008 00:32:25 -0300 Subject: [PATCH] Add logging configuration capabilities (closes #7). Add an option (log-config-files) to specify logging (standard Python's) configuration files to use to configure the logging system. --- pymind | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/pymind b/pymind index 3857ef8..da7b218 100755 --- a/pymind +++ b/pymind @@ -1,14 +1,10 @@ #!/usr/bin/env python # vim: set encoding=utf-8 et sw=4 sts=4 : -import logging ; log = logging.getLogger('pymind') -# First of all, we need to setup the logging framework -# FIXME: this should go in a configuration file -logging.basicConfig( - level = logging.DEBUG, - format = '%(asctime)s %(name)-24s %(levelname)-8s %(message)s', - datefmt = '%a, %d %b %Y %H:%M:%S', -) +import logging, logging.config ; log = logging.getLogger('pymind') +# default logging configuration +# (this is used before the user configuration file gets parsed) +logging.basicConfig(format='%(levelname)s: %(message)s') import os import sys @@ -35,6 +31,14 @@ config_file_paths = [ os.path.expanduser('~/.pymin/pymin.ini'), ] +# default locations where to look for the log configuration file +# all found files will be processed, overriding the previous configuration +# files values. +log_file_paths = [ + '/etc/pymin/log.ini', + os.path.expanduser('~/.pymin/log.ini'), +] + # default locations where to look for service plug-ins # search stops when a service plug-in is found services_paths = [ @@ -62,6 +66,8 @@ options.init('pymind', 'Pymin daemon global options', [ metavar='SERVICE', help="manage service SERVICE"), ListOption('services_dirs', V.String, 'd', default=[], metavar='DIR', help="search for services in DIR"), + ListOption('log_config_files', V.String, 'l', default=log_file_paths, + metavar='FILE', help="load log configuration FILE"), ConfigOption('config_file', 'c', metavar='FILE', help="load the configuration file FILE after the default " "configuration files"), @@ -126,11 +132,27 @@ def build_root(config, args, services): return root +def setup_logging(config_files): + loaded_files = 0 + for f in config_files: + try: + f = open(f) + except Exception, e: + log.info("config file '%s' can't be readed (%s)", f, e) + continue + logging.config.fileConfig(f) + f.close() + loaded_files += 1 + if not loaded_files: + log.warning('no log config file loaded') + + def main(): services = Services() (config, args) = get_config(config_file_paths, '%prog 0.1', 'Router services administration daemon', services.add_config_options, config_defaults) + setup_logging(config.log_config_files) root_handler = build_root(config, args, services.services) activate_ip_forward() PyminDaemon(root_handler, (config.bind_addr, config.bind_port)).run() -- 2.43.0