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 

23 def testLogLevels(self): 

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

25 

26 root = getLogger() 

27 

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) 

32 

33 def testLogCommands(self): 

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

35 

36 root = getLogger() 

37 

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") 

47 

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

49 

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") 

55 

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) 

62 

63 child = root.getChild("child") 

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

65 child.setLevel(root.DEBUG) 

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

67 

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) 

75 

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) 

85 

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) 

90 

91 

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

93 unittest.main()