CORD-1743: Some unit tests + fixed config bugs
Change-Id: I1e8c95334184662f245572d0d2cd1431b8bada9d
diff --git a/multistructlog.py b/multistructlog.py
index 8896cf2..b4aa19b 100644
--- a/multistructlog.py
+++ b/multistructlog.py
@@ -43,13 +43,11 @@
import sys
import copy
-
PROCESSOR_MAP = {
'StreamHandler': structlog.dev.ConsoleRenderer(),
'LogstashHandler': structlog.processors.JSONRenderer(),
}
-
class FormatterFactory:
def __init__(self, handler_name):
self.handler_name = handler_name
@@ -66,13 +64,9 @@
class XOSLoggerFactory:
- def __init__(self, handlers):
- self.handlers = handlers
-
def __call__(self):
base_logger = logging.getLogger()
- base_logger.handlers = []
- for h in self.handlers:
+ for h in base_logger.handlers:
formatter = FormatterFactory(h.__class__.__name__)()
h.setFormatter(formatter)
base_logger.addHandler(h)
@@ -101,7 +95,7 @@
"""Inherit base options from config"""
try:
- logging_config = copy.deepcopy(_config.get('logging'))
+ logging_config = copy.deepcopy(_config)
except AttributeError:
first_entry_elts.append('Config is empty')
logging_config = {}
@@ -128,7 +122,6 @@
logstash.LogstashHandler('localhost', 5617, version=1)
]
- handlers = logging_config.get('handlers', default_handlers)
logging.config.dictConfig(logging_config)
# Processors
@@ -140,7 +133,7 @@
structlog.stdlib.ProcessorFormatter.wrap_for_formatter
])
- factory = XOSLoggerFactory(handlers)
+ factory = XOSLoggerFactory()
structlog.configure(
processors=processors,
diff --git a/tests/test_logger.py b/tests/test_logger.py
new file mode 100644
index 0000000..f76902c
--- /dev/null
+++ b/tests/test_logger.py
@@ -0,0 +1,81 @@
+
+# Copyright 2017-present Open Networking 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.
+
+
+import unittest
+import mock
+import pdb
+import os, sys
+import multistructlog
+import logging
+
+class MockLogging:
+ def debug(self, str):
+ return str
+
+class TestMultiStructLog(unittest.TestCase):
+ def setUp(self):
+ self.logging_config= {
+ 'version': 1,
+ 'handlers': {
+ 'default': {
+ 'class': 'logging.StreamHandler',
+ },
+ },
+
+ 'loggers': {
+ '': {
+ 'handlers': ['default'],
+ 'level': 'INFO',
+ 'propagate': True
+ },
+ }
+ }
+
+ self.config = {'logging':self.logging_config}
+
+ @mock.patch('multistructlog.logging')
+ def test_reload(self, mock_logging):
+ logger = multistructlog.create_logger({'logging':{'version':1, 'foo':'bar'}})
+ logger0 = multistructlog.create_logger({'logging':{'version':1, 'foo':'bar'}})
+ logger2 = multistructlog.create_logger({'logging':{'version':1, 'foo':'notbar'}})
+ self.assertEqual(logger, logger0)
+ self.assertNotEqual(logger,logger2)
+
+ # "Starting" is only printed once
+ self.assertEqual(mock_logging.StreamHandler.call_count, 2)
+
+ @mock.patch('multistructlog.logging')
+ def test_level(self, mock_logging):
+ logger = multistructlog.create_logger({'logging':{'version':1, 'foo':'x'}})
+ logger.info('Test 1')
+ logger.debug('Test 2')
+
+ # Default level is INFO
+ self.assertEqual(mock_logging.StreamHandler.call_count, 1)
+
+ @mock.patch('multistructlog.logging')
+ def test_override_level(self, mock_logging):
+ self.config['logging']['loggers']['']['level'] = 'DEBUG'
+ logger = multistructlog.create_logger(self.config)
+
+ logger.info('Test 1')
+ logger.debug('Test 2')
+ pdb.set_trace()
+
+ self.assertEqual(mock_logging.StreamHandler.call_count, 2)
+
+if __name__ == '__main__':
+ unittest.main()