Add log formatting option and dynamic Log-Level change Implement MDC Log entry format...
[com/pylog.git] / mdclogpy / tst / test_mdclogpy.py
1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
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 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 # platform project (RICP).
18 #
19 """Unit tests for mdclogpy root logger"""
20 import unittest
21 from unittest.mock import patch
22 import sys
23
24 import mdclogpy
25 from .mdclogtestutils import TestMdcLogUtils
26
27
28 class TestMdcLog(unittest.TestCase):
29     """Unit tests for mdclog.py"""
30
31     def setUp(self):
32         self.prog_id = sys.argv[0]
33
34     def tearDown(self):
35         pass
36
37
38     @patch('mdclogpy.Logger._output_log')
39     def test_that_root_logger_logs_the_message_using_the_proc_name(self, output_mock):
40
41         mdclogpy.log(mdclogpy.Level.DEBUG, "This is a test log")
42         mdclogpy.error("This is an error log")
43         mdclogpy.warning("This is a warning log")
44         mdclogpy.info("This is an info log")
45         mdclogpy.debug("This is a debug log")
46
47         logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
48         self.assertEqual(self.prog_id, logs[0]["id"])
49         self.assertEqual(self.prog_id, logs[1]["id"])
50         self.assertEqual(self.prog_id, logs[2]["id"])
51         self.assertEqual(self.prog_id, logs[3]["id"])
52         self.assertEqual(self.prog_id, logs[4]["id"])
53         self.assertEqual("This is a test log", logs[0]["msg"])
54         self.assertEqual("This is an error log", logs[1]["msg"])
55         self.assertEqual("This is a warning log", logs[2]["msg"])
56         self.assertEqual("This is an info log", logs[3]["msg"])
57         self.assertEqual("This is a debug log", logs[4]["msg"])
58
59     def test_that_root_logger_get_level_returns_the_current_log_level(self):
60
61         
62         mdclogpy.set_level(mdclogpy.Level.INFO)
63         self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.INFO)
64         mdclogpy.set_level(mdclogpy.Level.WARNING)
65         self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.WARNING)
66         mdclogpy.set_level(mdclogpy.Level.ERROR)
67         self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.ERROR)
68         mdclogpy.set_level(mdclogpy.Level.DEBUG)
69         self.assertEqual(mdclogpy.get_level(), mdclogpy.Level.DEBUG)
70
71     @patch('mdclogpy.Logger._output_log')
72     def test_that_root_logger_logs_with_correct_criticality(self, output_mock):
73
74         mdclogpy.set_level(mdclogpy.Level.DEBUG)
75
76         mdclogpy.log(mdclogpy.Level.DEBUG, "debug test log")
77         mdclogpy.log(mdclogpy.Level.INFO, "info test log")
78         mdclogpy.log(mdclogpy.Level.WARNING, "warning test log")
79         mdclogpy.log(mdclogpy.Level.ERROR, "error test log")
80
81         mdclogpy.debug("another debug test log")
82         mdclogpy.info("another info test log")
83         mdclogpy.warning("another warning test log")
84         mdclogpy.error("another error test log")
85
86         logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
87         self.assertEqual(8, output_mock.call_count)
88         self.assertEqual(logs[0]["crit"], "DEBUG")
89         self.assertEqual(logs[1]["crit"], "INFO")
90         self.assertEqual(logs[2]["crit"], "WARNING")
91         self.assertEqual(logs[3]["crit"], "ERROR")
92         self.assertEqual(logs[4]["crit"], "DEBUG")
93         self.assertEqual(logs[5]["crit"], "INFO")
94         self.assertEqual(logs[6]["crit"], "WARNING")
95         self.assertEqual(logs[7]["crit"], "ERROR")
96
97     @patch('mdclogpy.Logger._output_log')
98     def test_that_root_logger_logs_mdc_values_correctly(self, output_mock):
99
100         mdclogpy.add_mdc("key1", "value1")
101         mdclogpy.add_mdc("key2", "value2")
102         mdclogpy.error("mdc test")
103
104         logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
105         self.assertEqual(logs[0]["mdc"]["key1"], "value1")
106         self.assertEqual(logs[0]["mdc"]["key2"], "value2")
107
108     @patch('mdclogpy.Logger._output_log')
109     def test_that_non_printable_characters_are_logged_correctly(self, output_mock):
110
111         mdclogpy.set_level(mdclogpy.Level.DEBUG)
112         mdclogpy.info("line feed\ntest")
113         mdclogpy.info("tab\ttest")
114         mdclogpy.info("carriage return\rtest")
115         logs = TestMdcLogUtils.get_logs_as_json(output_mock.call_args_list)
116         self.assertEqual(logs[0]["msg"], "line feed\ntest")
117         self.assertEqual(logs[1]["msg"], "tab\ttest")
118         self.assertEqual(logs[2]["msg"], "carriage return\rtest")
119
120 if __name__ == '__main__':
121     unittest.main()