Add python logger configuration

This adds a common usable logging configuration for DIB, that
can be used by different python tools or elements.

This change is a factored out patch from the block device
refactoring.

Change-Id: I34d44610f74030936e7c0f602340b183baec3f1b
Signed-off-by: Andreas Florath <andreas@florath.net>
This commit is contained in:
Andreas Florath 2016-06-03 18:37:35 +02:00 committed by Ian Wienand
parent d9525cfee9
commit 6ca6057981
2 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,97 @@
# Copyright 2016 Andreas Florath (andreas@florath.net)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Python Logging Configuration for DIB
# Usage:
# In the main (application) file, do an
# import logging_config
# ...
# logging_config.setup()
# It is then possible to use the normal python logging interface, like
# logger = logging.getLogger(__name__)
# logger.info("Info Message")
import json
import logging
import logging.config
import os
import sys
# A simple formatter to more or less copy oslo.log's ContextFormatter
class DibFormatter(logging.Formatter):
def __init__(self, *args, **kwargs):
self.fmt = kwargs['fmt']
self.debug_suffix = kwargs.pop('debug_suffix')
logging.Formatter.__init__(self, *args, **kwargs)
def format(self, record):
if record.levelno == logging.DEBUG:
fmt = self.fmt + self.debug_suffix
else:
fmt = self.fmt
if sys.version_info < (3, 2):
self._fmt = fmt
else:
self._style = logging.PercentStyle(fmt)
self._fmt = self._style._fmt
return logging.Formatter.format(self, record)
def setup():
# Check for the DIB_DEBUG_TRACE environment variable
# If it is set to something greater then 0, use debug
# logging.
LOG_LEVEL = "DEBUG" if 'DIB_DEBUG_TRACE' in os.environ \
and int(os.environ['DIB_DEBUG_TRACE']) > 0 else "INFO"
# Default logging configuration which can be overwritten
# by a config file passed in by a user.
PYTHON_LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'()': 'diskimage_builder.logging_config.DibFormatter',
'datefmt': '%Y-%m-%d %H:%M:%S',
'fmt': '%(asctime)s.%(msecs)03d %(levelname)s '
'%(name)s [-] %(message)s',
'debug_suffix': ' %(funcName)s %(pathname)s:%(lineno)d'
}
},
'handlers': {
'default': {
'level': LOG_LEVEL,
'class': 'logging.StreamHandler',
'formatter': 'standard',
}
},
'loggers': {
'': {
'handlers': ['default'],
'level': LOG_LEVEL,
'propagate': True
}
}
}
if 'DIB_PYTHON_LOGGING_CONFIG_FILE' in os.environ:
with open(os.environ['DIB_PYTHON_LOGGING_CONFIG_FILE']) as fd:
PYTHON_LOGGING_CONFIG = json.load(fd)
logging.config.dictConfig(PYTHON_LOGGING_CONFIG)

View File

@ -0,0 +1,29 @@
# Copyright 2016 Andreas Florath (andreas@florath.net)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import testtools
from diskimage_builder import logging_config
class TestLoggingConfig(testtools.TestCase):
def test_defaults(self):
logging_config.setup()
log = logging.getLogger(__name__)
log.debug("Debug Message")
log.info("Info Message")
log.warning("Warning Message")
log.error("Error Message")