Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/websocket/_logging.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

53 statements  

1import logging 

2from typing import Any, Optional 

3 

4""" 

5_logging.py 

6websocket - WebSocket client library for Python 

7 

8Copyright 2025 engn33r 

9 

10Licensed under the Apache License, Version 2.0 (the "License"); 

11you may not use this file except in compliance with the License. 

12You may obtain a copy of the License at 

13 

14 http://www.apache.org/licenses/LICENSE-2.0 

15 

16Unless required by applicable law or agreed to in writing, software 

17distributed under the License is distributed on an "AS IS" BASIS, 

18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

19See the License for the specific language governing permissions and 

20limitations under the License. 

21""" 

22 

23_logger = logging.getLogger("websocket") 

24try: 

25 from logging import NullHandler 

26except ImportError: 

27 

28 class NullHandler(logging.Handler): # type: ignore[no-redef] 

29 def emit(self, record: Any) -> None: 

30 pass 

31 

32 

33_logger.addHandler(NullHandler()) 

34 

35_traceEnabled = False 

36_trace_handler: Optional[logging.Handler] = None 

37_default_level = _logger.getEffectiveLevel() 

38 

39__all__ = [ 

40 "enableTrace", 

41 "dump", 

42 "error", 

43 "warning", 

44 "debug", 

45 "trace", 

46 "isEnabledForError", 

47 "isEnabledForDebug", 

48 "isEnabledForTrace", 

49] 

50 

51 

52def enableTrace( 

53 traceable: bool, 

54 handler: Optional[logging.Handler] = None, 

55 level: str = "DEBUG", 

56) -> None: 

57 """ 

58 Turn on/off the traceability. 

59 

60 Parameters 

61 ---------- 

62 traceable: bool 

63 If set to True, traceability is enabled. 

64 """ 

65 global _traceEnabled, _trace_handler 

66 if traceable: 

67 if handler is None: 

68 handler = logging.StreamHandler() 

69 if _trace_handler and _trace_handler in _logger.handlers: 

70 _logger.removeHandler(_trace_handler) 

71 _trace_handler = handler 

72 if handler not in _logger.handlers: 

73 _logger.addHandler(handler) 

74 _logger.setLevel(getattr(logging, level)) 

75 _traceEnabled = True 

76 else: 

77 _traceEnabled = False 

78 if _trace_handler and _trace_handler in _logger.handlers: 

79 _logger.removeHandler(_trace_handler) 

80 _trace_handler = None 

81 _logger.setLevel(_default_level) 

82 

83 

84def dump(title: str, message: str) -> None: 

85 if _traceEnabled: 

86 _logger.debug(f"--- {title} ---") 

87 _logger.debug(message) 

88 _logger.debug("-----------------------") 

89 

90 

91def error(msg: str) -> None: 

92 _logger.error(msg) 

93 

94 

95def warning(msg: str) -> None: 

96 _logger.warning(msg) 

97 

98 

99def debug(msg: str) -> None: 

100 _logger.debug(msg) 

101 

102 

103def info(msg: str) -> None: 

104 _logger.info(msg) 

105 

106 

107def trace(msg: str) -> None: 

108 if _traceEnabled: 

109 _logger.debug(msg) 

110 

111 

112def isEnabledForError() -> bool: 

113 return _logger.isEnabledFor(logging.ERROR) 

114 

115 

116def isEnabledForDebug() -> bool: 

117 return _logger.isEnabledFor(logging.DEBUG) 

118 

119 

120def isEnabledForTrace() -> bool: 

121 return _traceEnabled