Author: reinhard Date: 2007-07-13 17:22:52 -0500 (Fri, 13 Jul 2007) New Revision: 9759
Added: trunk/gnue-common/src/base/utils.py Modified: trunk/gnue-common/src/base/log.py Log: Allow for logging configuration files. issue123 in-progress Modified: trunk/gnue-common/src/base/log.py =================================================================== --- trunk/gnue-common/src/base/log.py 2007-07-12 23:08:03 UTC (rev 9758) +++ trunk/gnue-common/src/base/log.py 2007-07-13 22:22:52 UTC (rev 9759) @@ -25,24 +25,29 @@ # pylint: disable-msg=W0142 """ -The logging module extends the standard logging module with some convenience +The log module extends the standard logging module with some convenience functions related to the logging schema used in GNU Enterprise. """ import inspect import logging +import logging.config +from gnue.common.base import utils + __all__ = ['debug', 'info', 'warning', 'deprecated', 'error', 'critical', 'exception', 'debug_n', 'info_n', 'warning_n', 'deprecated_n', 'error_n', 'critical_n', 'exception_n'] - # TODO: -# - Read debug config # - function for enter() and leave(), maybe as decorator? # - function to declare function as deprecated, maybe as decorator? # - Exception hook +# - allow for __gnue_logger__ module global variable to use as a logger name +# instead of the module name (make a function in utils.py to search and cache +# module global variables in the module hierarchy) + # ----------------------------------------------------------------------------- # Log to the logger named after the calling module # ----------------------------------------------------------------------------- @@ -229,6 +234,7 @@ frame = frame.f_back return ("(unknown file)", 0, "(unknown function)") + # ----------------------------------------------------------------------------- # Module initialization # ----------------------------------------------------------------------------- @@ -236,8 +242,13 @@ # Use our own Logger class. logging.setLoggerClass(Logger) -# Configure logging to a very basic default behaviour to start with. -logging.basicConfig(level=10) - # Add level for deprecation warnings. logging.addLevelName(31, 'DEPRECATED') + +# Configure logging through configuration files, or use standard configuration +# if no configuration files available. +_CONFIG_FILES = utils.config_files('log') +if _CONFIG_FILES: + logging.config.fileConfig(_CONFIG_FILES) +else: + logging.basicConfig() Added: trunk/gnue-common/src/base/utils.py =================================================================== --- trunk/gnue-common/src/base/utils.py 2007-07-12 23:08:03 UTC (rev 9758) +++ trunk/gnue-common/src/base/utils.py 2007-07-13 22:22:52 UTC (rev 9759) @@ -0,0 +1,73 @@ +# GNU Enterprise Common Library - Basic Utilities +# +# Copyright 2001-2007 Free Software Foundation +# +# This file is part of GNU Enterprise +# +# GNU Enterprise is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public +# License as published by the Free Software Foundation; either +# version 2, or (at your option) any later version. +# +# GNU Enterprise is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +# PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with program; see the file COPYING. If not, +# write to the Free Software Foundation, Inc., 59 Temple Place +# - Suite 330, Boston, MA 02111-1307, USA. +# +# $Id$ + +""" +The utils module provides some very basic although GNU Enterprise specific +utility functions. +""" + +import os.path + +from gnue import paths + +__all__ = ['config_files'] + + +# ----------------------------------------------------------------------------- +# Return a list of config files to use +# ----------------------------------------------------------------------------- + +def config_files(name): + """ + Return a list of filenames to parse for a specific configuration file. + + The list contains all existing config files in the following order of + directories searched: + 1. system configuration (usually /etc/gnue/*.conf) + 2. user configuration (~/.gnue/*.conf) + 3. local configuration (etc/*.conf) + 4. environment variable location ($GNUE_*) + 5. fixed system configuration (/etc/gnue/*.conf.fixed) + The practical use for 4. is to define configuration entries that cannot be + overwritten by normal users. + + For example, if name is C{connections}, on a standard install on a POSIX + system, the file would be searched in: + 1. C{/etc/gnue/connections.conf} + 2. C{~/.gnue/connections.conf} + 3. C{etc/connections.conf} (from the current directory) + 4. the content of the C{GNUE_CONNECTIONS} environment variable + 5. C{/etc/gnue/connections.conf.fixed} + + @param name: name of the configuration file without the .conf ending + @return: list of full path names of configuration files to parse + """ + files = [] + files.append(os.path.join(paths.config, name + '.conf')) + if os.environ.has_key('HOME'): + files.append(os.path.join(os.environ['HOME'], name + '.conf')) + if os.environ.has_key('GNUE_' + name.upper()): + files.append(os.environ['GNUE_' + name.upper()]) + files.append(os.path.join('etc', name + '.conf')) + files.append(os.path.join(paths.config, name + '.conf.fixed')) + return [f for f in files if os.path.isfile(f)] Property changes on: trunk/gnue-common/src/base/utils.py ___________________________________________________________________ Name: svn:keywords + Id _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue