summaryrefslogtreecommitdiffstats
path: root/src/python
diff options
context:
space:
mode:
authorMichal Minar <miminar@redhat.com>2013-09-25 15:28:35 +0200
committerMichal Minar <miminar@redhat.com>2013-09-25 17:00:46 +0200
commit0d5670dc1fdc08d863a230f7b6423155d6c53986 (patch)
treeb9bc0b72f2caec9dad256900e87fcad6ec298755 /src/python
parentcb5d40a7166ea5f4566ce19de49780656c69b1b4 (diff)
downloadopenlmi-providers-0d5670dc1fdc08d863a230f7b6423155d6c53986.tar.gz
openlmi-providers-0d5670dc1fdc08d863a230f7b6423155d6c53986.tar.xz
openlmi-providers-0d5670dc1fdc08d863a230f7b6423155d6c53986.zip
python: support for older versions of python (2.6)
dictConfig() function of logging.config module is not present in stdlib of python 2.6. This change replaces mentioned function call with manual setup of handlers and modules.
Diffstat (limited to 'src/python')
-rw-r--r--src/python/lmi/providers/cmpi_logging.py140
1 files changed, 67 insertions, 73 deletions
diff --git a/src/python/lmi/providers/cmpi_logging.py b/src/python/lmi/providers/cmpi_logging.py
index c1e29c9..1199401 100644
--- a/src/python/lmi/providers/cmpi_logging.py
+++ b/src/python/lmi/providers/cmpi_logging.py
@@ -23,7 +23,6 @@ under cimom.
"""
import logging
-import logging.config
import inspect
from itertools import chain
import os
@@ -34,7 +33,7 @@ TRACE_WARNING = logging.DEBUG - 1
TRACE_INFO = logging.DEBUG - 2
TRACE_VERBOSE = logging.DEBUG - 3
-# Mapping from level name to its number
+#: Mapping from level name to its number.
LOGGING_LEVELS = {
"critical" : logging.CRITICAL,
"error" : logging.ERROR,
@@ -47,41 +46,15 @@ LOGGING_LEVELS = {
"trace_verbose" : TRACE_VERBOSE
}
-DEFAULT_LOGGING_CONFIG = {
- "version" : 1,
- 'disable_existing_loggers' : True,
- "formatters": {
- # this is a message format for logging function/method calls
- # it's manually set up in YumWorker's init method
- "default": {
- "()": "lmi.providers.cmpi_logging.DispatchingFormatter",
- "formatters" : {
- "lmi.providers.cmpi_logging.trace_function_or_method":
- "%(levelname)s:%(message)s"
- },
- "default" : "%(levelname)s:%(module)s:"
- "%(funcName)s:%(lineno)d - %(message)s"
- },
- },
- "handlers": {
- "stderr" : {
- "class" : "logging.StreamHandler",
- "level" : "ERROR",
- "formatter": "default",
- },
- "cmpi" : {
- "()" : "lmi.providers.cmpi_logging.CMPILogHandler",
- # this must be filled with broker env logger
- "cmpi_logger" : None,
- "level" : "ERROR",
- "formatter" : "default"
- },
- },
- "root": {
- "level": "ERROR",
- "handlers" : ["cmpi"],
- },
- }
+#: This associates special format strings to various logger names
+SPECIAL_FORMAT_STRINGS = {
+ "lmi.providers.cmpi_logging.trace_function_or_method" :
+ "%(levelname)s:%(message)s"
+}
+
+#: Default format string to use in stderr and cmpi handlers.
+DEFAULT_FORMAT_STRING = \
+ "%(levelname)s:%(module)s:%(funcName)s:%(lineno)d - %(message)s"
class DispatchingFormatter(object):
"""
@@ -315,24 +288,14 @@ def trace_method(func, frame_level=1):
""" Convenience function used for decorating methods. """
return trace_function_or_method(True, frame_level + 1)(func)
-def setup(env, config, default_logging_dict=None):
+def try_setup_from_file(config):
"""
- Set up the logging with options given by SoftwareConfiguration instance.
- This should be called at process's startup before any message is sent to
- log.
-
- :param env: (``ProviderEnvironment``) Provider environment, taken
- from CIMOM callback (e.g. ``get_providers()``).
- :param config: (``BaseConfiguration``) Configuration with Log section
- containing settings for logging.
- :param default_logging_dict: (``dict``) Dictionary with default logging
- configuration. If ``None``, ``DEFAULT_LOGGING_CONFIG`` will be used. If
- given, it must contain at least "root" entry for root logger and
- predefined "cmpi" and "stderr" handlers for logging to CIMOM and to
- stderr.
+ Try to configure logging with a file specified in configuration.
+
+ :returns: ``True`` if the file configuration is given, successfuly
+ parsed and carried out.
+ :rtype: boolean
"""
- cp = config.config
- logging_setup = False
try:
path = config.file_path('Log', 'FileConfig')
if not os.path.exists(path):
@@ -340,29 +303,60 @@ def setup(env, config, default_logging_dict=None):
' not exist', path)
else:
logging.config.fileConfig(path)
- logging_setup = True
+ return True
except Exception:
- if cp.has_option('Log', 'FileConfig'):
+ if config.config.has_option('Log', 'FileConfig'):
logging.getLogger(__name__).exception(
'failed to setup logging from FileConfig')
- if logging_setup is False:
- if default_logging_dict is None:
- defaults = DEFAULT_LOGGING_CONFIG.copy()
- else:
- defaults = default_logging_dict
- defaults["handlers"]["cmpi"]["cmpi_logger"] = env.get_logger()
- if config.stderr:
- defaults["root"]["handlers"].append("stderr")
- level = config.logging_level
- if not level in LOGGING_LEVELS:
- logging.getLogger(__name__).error(
- 'level name "%s" not supported', level)
- else:
- level = LOGGING_LEVELS[level]
- for handler in defaults["handlers"].values():
- handler["level"] = level
- defaults["root"]["level"] = level
- logging.config.dictConfig(defaults)
+ return False
+
+def setup(env, config, special_format_strings=None):
+ """
+ Set up the logging with options stored in
+ :py:class:`lmi.base.BaseConfiguration` instance. This should be called
+ at provider's startup before any message is sent to log.
+
+ :param ProviderEnvironment env: Provider environment, taken from CIMOM
+ callback (e.g. ``get_providers()``).
+ :param config: Configuration with Log section containing settings for
+ logging.
+ :type config: :py:class:`lmi.base.BaseConfiguration`
+ :param dictionary special_format_strings: Assignes to various loggers
+ special format strings. It overrides pairs in
+ :py:data:`SPECIAL_FORMAT_STRINGS`. Its format is following: ::
+
+ { ( 'logger_name' : 'format_string_to_use' ), ... }
+ """
+ if ( special_format_strings is not None
+ and not isinstance(special_format_strings, dict)):
+ raise TypeError("special_format_strings must be a dictionary")
+ if try_setup_from_file(config):
+ return
+ logging_level = logging.ERROR
+ if not config.logging_level in LOGGING_LEVELS:
+ logging.getLogger(__name__).error(
+ 'level name "%s" not supported', config.logging_level)
+ else:
+ logging_level = LOGGING_LEVELS[config.logging_level]
+ logger = logging.getLogger()
+ logger.setLevel(logging_level)
+ # remove any previously set handlers
+ for handler in logger.handlers[:]:
+ logger.removeHandler(handler)
+ format_strings = SPECIAL_FORMAT_STRINGS.copy()
+ if special_format_strings is not None:
+ format_strings.update(special_format_strings)
+ # set up new ones
+ if config.stderr:
+ err_handler = logging.StreamHandler()
+ err_handler.setLevel(logging_level)
+ err_handler.setFormatter(
+ DispatchingFormatter(format_strings, DEFAULT_FORMAT_STRING))
+ logger.addHandler(err_handler)
+ cmpi_handler = CMPILogHandler(env.get_logger(), logging_level)
+ cmpi_handler.setFormatter(
+ DispatchingFormatter(format_strings, DEFAULT_FORMAT_STRING))
+ logger.addHandler(cmpi_handler)
class LogManager(object):
"""