Coverage for tests/test_logging.py: 17%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of utils.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12"""Simple unit test for Task logging.
13"""
15import logging
16import unittest
18from lsst.utils.logging import getLogger, trace_set_at
21class TestLogging(unittest.TestCase):
23 def testLogLevels(self):
24 """Check that the new log levels look reasonable."""
26 root = getLogger()
28 self.assertEqual(root.DEBUG, logging.DEBUG)
29 self.assertGreater(root.VERBOSE, logging.DEBUG)
30 self.assertLess(root.VERBOSE, logging.INFO)
31 self.assertLess(root.TRACE, logging.DEBUG)
33 def testLogCommands(self):
34 """Check that all the log commands work."""
36 root = getLogger()
38 with self.assertLogs(level=root.TRACE) as cm:
39 root.trace("Trace")
40 root.debug("Debug")
41 root.verbose("Verbose")
42 root.info("Info")
43 root.warning("Warning")
44 root.fatal("Fatal")
45 root.critical("Critical")
46 root.error("Error")
48 self.assertEqual(len(cm.records), 8)
50 # Check that each record has an explicit level name rather than
51 # "Level N" and comes from this file (and not the logging.py).
52 for record in cm.records:
53 self.assertRegex(record.levelname, "^[A-Z]+$")
54 self.assertEqual(record.filename, "test_logging.py")
56 with self.assertLogs(level=root.DEBUG) as cm:
57 # Should only issue the INFO message.
58 with root.temporary_log_level(root.INFO):
59 root.info("Info")
60 root.debug("Debug")
61 self.assertEqual(len(cm.records), 1)
63 child = root.getChild("child")
64 self.assertEqual(child.getEffectiveLevel(), root.getEffectiveLevel())
65 child.setLevel(root.DEBUG)
66 self.assertNotEqual(child.getEffectiveLevel(), root.getEffectiveLevel())
68 def testTraceSetAt(self):
69 log_name = "lsst.afw"
70 trace_set_at(log_name, 2)
71 trace2_log = getLogger(f"TRACE2.{log_name}")
72 trace3_log = getLogger(f"TRACE3.{log_name}")
73 self.assertEqual(trace2_log.getEffectiveLevel(), logging.DEBUG)
74 self.assertEqual(trace3_log.getEffectiveLevel(), logging.INFO)
76 # Check that child loggers are affected.
77 log_name = "lsst.daf"
78 child3_log = getLogger("TRACE3.lsst.daf")
79 child2_log = getLogger("TRACE2.lsst.daf")
80 self.assertEqual(child3_log.getEffectiveLevel(), logging.WARNING)
81 self.assertEqual(child2_log.getEffectiveLevel(), logging.WARNING)
82 trace_set_at("lsst", 2)
83 self.assertEqual(child3_log.getEffectiveLevel(), logging.INFO)
84 self.assertEqual(child2_log.getEffectiveLevel(), logging.DEBUG)
86 # Also check the root logger.
87 trace_set_at("", 3)
88 self.assertEqual(trace3_log.getEffectiveLevel(), logging.INFO)
89 self.assertEqual(getLogger("TRACE3.test").getEffectiveLevel(), logging.DEBUG)
92if __name__ == "__main__": 92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never true
93 unittest.main()