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
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
1import logging
2from typing import Any, Optional
4"""
5_logging.py
6websocket - WebSocket client library for Python
8Copyright 2025 engn33r
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
14 http://www.apache.org/licenses/LICENSE-2.0
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"""
23_logger = logging.getLogger("websocket")
24try:
25 from logging import NullHandler
26except ImportError:
28 class NullHandler(logging.Handler): # type: ignore[no-redef]
29 def emit(self, record: Any) -> None:
30 pass
33_logger.addHandler(NullHandler())
35_traceEnabled = False
36_trace_handler: Optional[logging.Handler] = None
37_default_level = _logger.getEffectiveLevel()
39__all__ = [
40 "enableTrace",
41 "dump",
42 "error",
43 "warning",
44 "debug",
45 "trace",
46 "isEnabledForError",
47 "isEnabledForDebug",
48 "isEnabledForTrace",
49]
52def enableTrace(
53 traceable: bool,
54 handler: Optional[logging.Handler] = None,
55 level: str = "DEBUG",
56) -> None:
57 """
58 Turn on/off the traceability.
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)
84def dump(title: str, message: str) -> None:
85 if _traceEnabled:
86 _logger.debug(f"--- {title} ---")
87 _logger.debug(message)
88 _logger.debug("-----------------------")
91def error(msg: str) -> None:
92 _logger.error(msg)
95def warning(msg: str) -> None:
96 _logger.warning(msg)
99def debug(msg: str) -> None:
100 _logger.debug(msg)
103def info(msg: str) -> None:
104 _logger.info(msg)
107def trace(msg: str) -> None:
108 if _traceEnabled:
109 _logger.debug(msg)
112def isEnabledForError() -> bool:
113 return _logger.isEnabledFor(logging.ERROR)
116def isEnabledForDebug() -> bool:
117 return _logger.isEnabledFor(logging.DEBUG)
120def isEnabledForTrace() -> bool:
121 return _traceEnabled