Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import unittest |
| 18 | import mock |
| 19 | import pdb |
| 20 | import os, sys |
| 21 | import multistructlog |
| 22 | import logging |
| 23 | |
| 24 | class MockLogging: |
| 25 | def debug(self, str): |
| 26 | return str |
| 27 | |
| 28 | class TestMultiStructLog(unittest.TestCase): |
| 29 | def setUp(self): |
| 30 | self.logging_config= { |
| 31 | 'version': 1, |
| 32 | 'handlers': { |
| 33 | 'default': { |
| 34 | 'class': 'logging.StreamHandler', |
| 35 | }, |
| 36 | }, |
| 37 | |
| 38 | 'loggers': { |
| 39 | '': { |
| 40 | 'handlers': ['default'], |
| 41 | 'level': 'INFO', |
| 42 | 'propagate': True |
Sapan Bhatia | 9ec41c2 | 2017-08-24 05:31:28 -0400 | [diff] [blame] | 43 | }, |
| 44 | } |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Sapan Bhatia | 9ec41c2 | 2017-08-24 05:31:28 -0400 | [diff] [blame] | 47 | self.config = self.logging_config |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 48 | |
| 49 | @mock.patch('multistructlog.logging') |
| 50 | def test_reload(self, mock_logging): |
Sapan Bhatia | 9ec41c2 | 2017-08-24 05:31:28 -0400 | [diff] [blame] | 51 | logger = multistructlog.create_logger({'version':1, 'foo':'bar'}) |
| 52 | logger0 = multistructlog.create_logger({'version':1, 'foo':'bar'}) |
| 53 | logger2 = multistructlog.create_logger({'version':1, 'foo':'notbar'}) |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 54 | self.assertEqual(logger, logger0) |
| 55 | self.assertNotEqual(logger,logger2) |
| 56 | |
| 57 | # "Starting" is only printed once |
| 58 | self.assertEqual(mock_logging.StreamHandler.call_count, 2) |
| 59 | |
| 60 | @mock.patch('multistructlog.logging') |
| 61 | def test_level(self, mock_logging): |
Sapan Bhatia | 9ec41c2 | 2017-08-24 05:31:28 -0400 | [diff] [blame] | 62 | logger = multistructlog.create_logger({'version':1, 'foo':'x'}) |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 63 | logger.info('Test 1') |
| 64 | logger.debug('Test 2') |
| 65 | |
| 66 | # Default level is INFO |
| 67 | self.assertEqual(mock_logging.StreamHandler.call_count, 1) |
| 68 | |
| 69 | @mock.patch('multistructlog.logging') |
| 70 | def test_override_level(self, mock_logging): |
Sapan Bhatia | 9ec41c2 | 2017-08-24 05:31:28 -0400 | [diff] [blame] | 71 | logger = multistructlog.create_logger(self.config, level='DEBUG') |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 72 | |
| 73 | logger.info('Test 1') |
| 74 | logger.debug('Test 2') |
Sapan Bhatia | e437cf4 | 2017-08-21 22:41:29 -0400 | [diff] [blame] | 75 | |
| 76 | self.assertEqual(mock_logging.StreamHandler.call_count, 2) |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | unittest.main() |