Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/websocket/_logging.py: 53%
38 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:34 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:34 +0000
1import logging
3"""
4_logging.py
5websocket - WebSocket client library for Python
7Copyright 2023 engn33r
9Licensed under the Apache License, Version 2.0 (the "License");
10you may not use this file except in compliance with the License.
11You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
15Unless required by applicable law or agreed to in writing, software
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
20"""
22_logger = logging.getLogger('websocket')
23try:
24 from logging import NullHandler
25except ImportError:
26 class NullHandler(logging.Handler):
27 def emit(self, record) -> None:
28 pass
30_logger.addHandler(NullHandler())
32_traceEnabled = False
34__all__ = ["enableTrace", "dump", "error", "warning", "debug", "trace",
35 "isEnabledForError", "isEnabledForDebug", "isEnabledForTrace"]
38def enableTrace(traceable: bool,
39 handler: logging.StreamHandler = logging.StreamHandler(),
40 level: str = "DEBUG") -> None:
41 """
42 Turn on/off the traceability.
44 Parameters
45 ----------
46 traceable: bool
47 If set to True, traceability is enabled.
48 """
49 global _traceEnabled
50 _traceEnabled = traceable
51 if traceable:
52 _logger.addHandler(handler)
53 _logger.setLevel(getattr(logging, level))
56def dump(title: str, message: str) -> None:
57 if _traceEnabled:
58 _logger.debug("--- " + title + " ---")
59 _logger.debug(message)
60 _logger.debug("-----------------------")
63def error(msg: str) -> None:
64 _logger.error(msg)
67def warning(msg: str) -> None:
68 _logger.warning(msg)
71def debug(msg: str) -> None:
72 _logger.debug(msg)
75def info(msg: str) -> None:
76 _logger.info(msg)
79def trace(msg: str) -> None:
80 if _traceEnabled:
81 _logger.debug(msg)
84def isEnabledForError() -> bool:
85 return _logger.isEnabledFor(logging.ERROR)
88def isEnabledForDebug() -> bool:
89 return _logger.isEnabledFor(logging.DEBUG)
92def isEnabledForTrace() -> bool:
93 return _traceEnabled