Coverage for tests/test_logging.py: 20%

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

55 statements  

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. 

11 

12"""Simple unit test for Task logging. 

13""" 

14 

15import logging 

16import unittest 

17 

18from lsst.utils.logging import getLogger, trace_set_at 

19 

20 

21class TestLogging(unittest.TestCase): 

22 def testLogLevels(self): 

23 """Check that the new log levels look reasonable.""" 

24 

25 root = getLogger() 

26 

27 self.assertEqual(root.DEBUG, logging.DEBUG) 

28 self.assertGreater(root.VERBOSE, logging.DEBUG) 

29 self.assertLess(root.VERBOSE, logging.INFO) 

30 self.assertLess(root.TRACE, logging.DEBUG) 

31 

32 def testLogCommands(self): 

33 """Check that all the log commands work.""" 

34 

35 root = getLogger() 

36 

37 with self.assertLogs(level=root.TRACE) as cm: 

38 root.trace("Trace") 

39 root.debug("Debug") 

40 root.verbose("Verbose") 

41 root.info("Info") 

42 root.warning("Warning") 

43 root.fatal("Fatal") 

44 root.critical("Critical") 

45 root.error("Error") 

46 

47 self.assertEqual(len(cm.records), 8) 

48 

49 # Check that each record has an explicit level name rather than 

50 # "Level N" and comes from this file (and not the logging.py). 

51 for record in cm.records: 

52 self.assertRegex(record.levelname, "^[A-Z]+$") 

53 self.assertEqual(record.filename, "test_logging.py") 

54 

55 with self.assertLogs(level=root.DEBUG) as cm: 

56 # Should only issue the INFO message. 

57 with root.temporary_log_level(root.INFO): 

58 root.info("Info") 

59 root.debug("Debug") 

60 self.assertEqual(len(cm.records), 1) 

61 

62 child = root.getChild("child") 

63 self.assertEqual(child.getEffectiveLevel(), root.getEffectiveLevel()) 

64 child.setLevel(root.DEBUG) 

65 self.assertNotEqual(child.getEffectiveLevel(), root.getEffectiveLevel()) 

66 

67 def testTraceSetAt(self): 

68 log_name = "lsst.afw" 

69 trace_set_at(log_name, 2) 

70 trace2_log = getLogger(f"TRACE2.{log_name}") 

71 trace3_log = getLogger(f"TRACE3.{log_name}") 

72 self.assertEqual(trace2_log.getEffectiveLevel(), logging.DEBUG) 

73 self.assertEqual(trace3_log.getEffectiveLevel(), logging.INFO) 

74 

75 # Check that child loggers are affected. 

76 log_name = "lsst.daf" 

77 child3_log = getLogger("TRACE3.lsst.daf") 

78 child2_log = getLogger("TRACE2.lsst.daf") 

79 self.assertEqual(child3_log.getEffectiveLevel(), logging.WARNING) 

80 self.assertEqual(child2_log.getEffectiveLevel(), logging.WARNING) 

81 trace_set_at("lsst", 2) 

82 self.assertEqual(child3_log.getEffectiveLevel(), logging.INFO) 

83 self.assertEqual(child2_log.getEffectiveLevel(), logging.DEBUG) 

84 

85 # Also check the root logger. 

86 trace_set_at("", 3) 

87 self.assertEqual(trace3_log.getEffectiveLevel(), logging.INFO) 

88 self.assertEqual(getLogger("TRACE3.test").getEffectiveLevel(), logging.DEBUG) 

89 

90 

91if __name__ == "__main__": 91 ↛ 92line 91 didn't jump to line 92, because the condition on line 91 was never true

92 unittest.main()