diff options
author | Michel Nederlof <mnederlof@cloudvps.com> | 2017-04-04 15:02:39 +0200 |
---|---|---|
committer | Michel Nederlof <mnederlof@cloudvps.com> | 2017-04-04 22:46:56 +0200 |
commit | 20437e5177256bd1ebbaafc8508fab395314ad48 (patch) | |
tree | 3b2807173a9ccdc2b88537198c683489a7d0af5f | |
parent | 07de1eed161be9304ca03d04b00168a01242b6ad (diff) | |
download | oslo-log-20437e5177256bd1ebbaafc8508fab395314ad48.tar.gz |
When record.args is None, it should not give an exception.
Also included a unit test which will validate that scenario.
Error occured at oslo_privsep implementation, which is fixed upstream:
https://github.com/openstack/oslo.privsep/commit/3c47348ced0d3ace1113ba8de8dff015792b0b89
Change-Id: I6dd34f80662d2733b37f8d8d1b9f81eb6161f346
-rw-r--r-- | oslo_log/formatters.py | 2 | ||||
-rw-r--r-- | oslo_log/tests/unit/test_custom_loghandler.py | 51 |
2 files changed, 52 insertions, 1 deletions
diff --git a/oslo_log/formatters.py b/oslo_log/formatters.py index 275ceac..157dea5 100644 --- a/oslo_log/formatters.py +++ b/oslo_log/formatters.py @@ -270,7 +270,7 @@ class ContextFormatter(logging.Formatter): if six.PY2: should_use_unicode = True - for arg in record.args: + for arg in record.args or []: try: six.text_type(arg) except UnicodeDecodeError: diff --git a/oslo_log/tests/unit/test_custom_loghandler.py b/oslo_log/tests/unit/test_custom_loghandler.py new file mode 100644 index 0000000..7f2998f --- /dev/null +++ b/oslo_log/tests/unit/test_custom_loghandler.py @@ -0,0 +1,51 @@ +# Copyright (c) 2016 OpenStack Foundation +# +# 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. + +"""Unit Tests for oslo.log with custom log handler""" + + +import logging + +from oslo_log import log + +from oslo_log.tests.unit.test_log import LogTestBase + + +class CustomLogHandler(logging.StreamHandler): + # Custom loghandler to mimick the error which was later fixed by + # https://github.com/openstack/oslo.privsep/commit/3c47348ced0d3ace1113ba8de8dff015792b0b89 + + def emit(self, record): + # Make args None; this was the error, which broke oslo_log formatting + record.args = None # This is intentionally wrong + + super(CustomLogHandler, self).emit(record) + + +class CustomLogHandlerTestCase(LogTestBase): + def setUp(self): + super(CustomLogHandlerTestCase, self).setUp() + self.config(logging_context_format_string="HAS CONTEXT " + "[%(request_id)s]: " + "%(message)s", + logging_default_format_string="NOCTXT: %(message)s", + logging_debug_format_suffix="--DBG") + self.log = log.getLogger('') # obtain root logger instead of 'unknown' + self._add_handler_with_cleanup(self.log, handler=CustomLogHandler) + self._set_log_level_with_cleanup(self.log, logging.DEBUG) + + def test_log(self): + message = 'foo' + self.log.info(message) + self.assertEqual("NOCTXT: %s\n" % message, self.stream.getvalue()) |