|
15 | 15 |
|
16 | 16 | import enum |
17 | 17 | import logging |
| 18 | +import logging.handlers |
18 | 19 | import os |
19 | 20 | import warnings |
20 | 21 | from typing import Any |
@@ -94,6 +95,46 @@ class _SDAMStatusMessage(str, enum.Enum): |
94 | 95 | ConnectionClosedReason.IDLE: "Connection was idle too long", |
95 | 96 | ConnectionCheckOutFailedReason.TIMEOUT: "Connection exceeded the specified timeout", |
96 | 97 | } |
| 98 | +_DEBUG_LOG_HANDLER = None |
| 99 | + |
| 100 | + |
| 101 | +# Custom logging handler to ensure we only flush the buffered logs on a test failure |
| 102 | +class LoggingHandler(logging.handlers.MemoryHandler): |
| 103 | + def __init__(self, capacity=1000, target=None): |
| 104 | + super().__init__(capacity, logging.DEBUG, target, flushOnClose=False) |
| 105 | + self.original_capacity = capacity |
| 106 | + |
| 107 | + def shouldFlush(self, record): |
| 108 | + if len(self.buffer) >= self.capacity: |
| 109 | + self.capacity = len(self.buffer) * 2 |
| 110 | + return False |
| 111 | + |
| 112 | + def flush(self): |
| 113 | + super().flush() |
| 114 | + self.capacity = self.original_capacity |
| 115 | + |
| 116 | + def clear(self): |
| 117 | + self.buffer.clear() |
| 118 | + self.capacity = self.original_capacity |
| 119 | + |
| 120 | + |
| 121 | +if os.environ.get("DEBUG_LOG"): |
| 122 | + FORMAT = "%(levelname)s %(name)s %(message)s" |
| 123 | + handler = logging.StreamHandler() |
| 124 | + handler.setFormatter(logging.Formatter(FORMAT)) |
| 125 | + _DEBUG_LOG_HANDLER = LoggingHandler(target=handler) |
| 126 | + |
| 127 | + _COMMAND_LOGGER.setLevel(logging.DEBUG) |
| 128 | + _SERVER_SELECTION_LOGGER.setLevel(logging.DEBUG) |
| 129 | + _CONNECTION_LOGGER.setLevel(logging.DEBUG) |
| 130 | + _CLIENT_LOGGER.setLevel(logging.DEBUG) |
| 131 | + _SDAM_LOGGER.setLevel(logging.DEBUG) |
| 132 | + |
| 133 | + _COMMAND_LOGGER.addHandler(_DEBUG_LOG_HANDLER) |
| 134 | + _SERVER_SELECTION_LOGGER.addHandler(_DEBUG_LOG_HANDLER) |
| 135 | + _CONNECTION_LOGGER.addHandler(_DEBUG_LOG_HANDLER) |
| 136 | + _CLIENT_LOGGER.addHandler(_DEBUG_LOG_HANDLER) |
| 137 | + _SDAM_LOGGER.addHandler(_DEBUG_LOG_HANDLER) |
97 | 138 |
|
98 | 139 |
|
99 | 140 | def _log_client_error() -> None: |
|
0 commit comments