Hide keyboard shortcuts

Hot-keys 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 daf_butler. 

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# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("VERBOSE", "ButlerMDC") 

25 

26import logging 

27 

28VERBOSE = (logging.INFO + logging.DEBUG) // 2 

29"""Verbose log level""" 

30 

31logging.addLevelName(VERBOSE, "VERBOSE") 

32 

33 

34class MDCDict(dict): 

35 """Dictionary for MDC data. 

36 

37 This is internal class used for better formatting of MDC in Python logging 

38 output. It behaves like `defaultdict(str)` but overrides ``__str__`` and 

39 ``__repr__`` method to produce output better suited for logging records. 

40 """ 

41 

42 def __getitem__(self, name: str) -> str: 

43 """Return value for a given key or empty string for missing key. 

44 """ 

45 return self.get(name, "") 

46 

47 def __str__(self) -> str: 

48 """Return string representation, strings are interpolated without 

49 quotes. 

50 """ 

51 items = (f"{k}={self[k]}" for k in sorted(self)) 

52 return "{" + ", ".join(items) + "}" 

53 

54 def __repr__(self) -> str: 

55 return str(self) 

56 

57 

58class ButlerMDC: 

59 """Handle setting and unsetting of global MDC records. 

60 

61 The Mapped Diagnostic Context (MDC) can be used to set context 

62 for log messages. 

63 

64 Currently there is one global MDC dict. Per-thread MDC is not 

65 yet supported. 

66 """ 

67 

68 _MDC = MDCDict() 

69 

70 @classmethod 

71 def MDC(cls, key: str, value: str) -> str: 

72 """Set MDC for this key to the supplied value. 

73 

74 Parameters 

75 ---------- 

76 key : `str` 

77 Key to modify. 

78 value : `str` 

79 New value to use. 

80 

81 Returns 

82 ------- 

83 old : `str` 

84 The previous value for this key. 

85 """ 

86 old_value = cls._MDC[key] 

87 cls._MDC[key] = value 

88 return old_value 

89 

90 @classmethod 

91 def MDCRemove(cls, key: str) -> None: 

92 """Clear the MDC value associated with this key. 

93 

94 Can be called even if the key is not known to MDC. 

95 """ 

96 cls._MDC.pop(key, None)