blob: 9e40ca7ed05e8a3b047d75a9a71f09598cd80fd7 [file] [log] [blame]
Sapan Bhatiae437cf42017-08-21 22:41:29 -04001
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
17import unittest
18import mock
19import pdb
20import os, sys
21import multistructlog
22import logging
23
24class MockLogging:
25 def debug(self, str):
26 return str
27
28class 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 Bhatia9ec41c22017-08-24 05:31:28 -040043 },
44 }
Sapan Bhatiae437cf42017-08-21 22:41:29 -040045 }
46
Sapan Bhatia9ec41c22017-08-24 05:31:28 -040047 self.config = self.logging_config
Sapan Bhatiae437cf42017-08-21 22:41:29 -040048
49 @mock.patch('multistructlog.logging')
50 def test_reload(self, mock_logging):
Sapan Bhatia9ec41c22017-08-24 05:31:28 -040051 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 Bhatiae437cf42017-08-21 22:41:29 -040054 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 Bhatia9ec41c22017-08-24 05:31:28 -040062 logger = multistructlog.create_logger({'version':1, 'foo':'x'})
Sapan Bhatiae437cf42017-08-21 22:41:29 -040063 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 Bhatia9ec41c22017-08-24 05:31:28 -040071 logger = multistructlog.create_logger(self.config, level='DEBUG')
Sapan Bhatiae437cf42017-08-21 22:41:29 -040072
73 logger.info('Test 1')
74 logger.debug('Test 2')
Sapan Bhatiae437cf42017-08-21 22:41:29 -040075
76 self.assertEqual(mock_logging.StreamHandler.call_count, 2)
77
78if __name__ == '__main__':
79 unittest.main()