1#
2# Copyright (C) 2010-2017 Vinay Sajip. See LICENSE.txt for details.
3#
4import logging
5from logutils.testing import TestHandler, Matcher
6import unittest
7
8class LoggingTest(unittest.TestCase):
9    def setUp(self):
10        self.handler = h = TestHandler(Matcher())
11        self.logger = l = logging.getLogger()
12        l.addHandler(h)
13
14    def tearDown(self):
15        self.logger.removeHandler(self.handler)
16        self.handler.close()
17
18    def test_simple(self):
19        "Simple test of logging test harness."
20        # Just as a demo, let's log some messages.
21        # Only one should show up in the log.
22        self.logger.debug("This won't show up.")
23        self.logger.info("Neither will this.")
24        self.logger.warning("But this will.")
25        h = self.handler
26        self.assertTrue(h.matches(levelno=logging.WARNING))
27        self.assertFalse(h.matches(levelno=logging.DEBUG))
28        self.assertFalse(h.matches(levelno=logging.INFO))
29
30    def test_partial(self):
31        "Test of partial matching in logging test harness."
32        # Just as a demo, let's log some messages.
33        # Only one should show up in the log.
34        self.logger.debug("This won't show up.")
35        self.logger.info("Neither will this.")
36        self.logger.warning("But this will.")
37        h = self.handler
38        self.assertTrue(h.matches(msg="ut th")) # from "But this will"
39        self.assertTrue(h.matches(message="ut th")) # from "But this will"
40        self.assertFalse(h.matches(message="either"))
41        self.assertFalse(h.matches(message="won't"))
42
43    def test_multiple(self):
44        "Test of matching multiple values in logging test harness."
45        # Just as a demo, let's log some messages.
46        # Only one should show up in the log.
47        self.logger.debug("This won't show up.")
48        self.logger.info("Neither will this.")
49        self.logger.warning("But this will.")
50        self.logger.error("And so will this.")
51        h = self.handler
52        self.assertTrue(h.matches(levelno=logging.WARNING,
53                                  message='ut thi'))
54        self.assertTrue(h.matches(levelno=logging.ERROR,
55                                  message='nd so wi'))
56        self.assertFalse(h.matches(levelno=logging.INFO))
57
58if __name__ == '__main__':
59    unittest.main()
60