Skip to content

Commit 25a4a1d

Browse files
committed
PYTHON-5292 - Debug logs should only output on test failure
1 parent bf6d95d commit 25a4a1d

File tree

7 files changed

+6
-70
lines changed

7 files changed

+6
-70
lines changed

.evergreen/scripts/run_tests.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def run() -> None:
173173
return
174174

175175
# Run local tests.
176-
print(f"Running tests with args: {TEST_ARGS + sys.argv[1:]}")
177176
ret = pytest.main(TEST_ARGS + sys.argv[1:])
178177
if ret != 0:
179178
sys.exit(ret)

.evergreen/scripts/setup_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def handle_test_env() -> None:
465465
UV_ARGS.append(f"--group {framework}")
466466

467467
else:
468-
TEST_ARGS = f"-v --capture=no --show-capture=no --durations=5 {TEST_ARGS}"
468+
TEST_ARGS = f"-v --durations=5 {TEST_ARGS}"
469469
TEST_SUITE = TEST_SUITE_MAP.get(test_name)
470470
if TEST_SUITE:
471471
TEST_ARGS = f"-m {TEST_SUITE} {TEST_ARGS}"

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ If you are running one of the `no-responder` tests, omit the `run-server` step.
351351

352352
## Enable Debug Logs
353353

354-
- Use `-o log_cli_level="DEBUG" -o log_cli=1` with `just test` or `pytest`.
355-
- Add `log_cli_level = "DEBUG` and `log_cli = 1` to the `tool.pytest.ini_options` section in `pyproject.toml` for Evergreen patches or to enable debug logs by default on your machine.
356-
- You can also set `DEBUG_LOG=1` and run either `just setup-tests` or `just-test`.
354+
- Use `-o log_cli_level="DEBUG" -o log_cli=1` with `just test` or `pytest` to output all debug logs to the terminal. **Warning**: This will output a huge amount of logs.
355+
- Add `log_cli_level = "DEBUG` to the `tool.pytest.ini_options` section in `pyproject.toml` to enable debug logs in this manner by default on your machine.
356+
- Set `DEBUG_LOG=1` and run `just setup-tests`, `just-test`, or `pytest` to enable debug logs only for failed tests.
357357
- Finally, you can use `just setup-tests --debug-log`.
358-
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for the patch.
358+
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for failed tests in the patch.
359359

360360
## Adding a new test suite
361361

pymongo/logger.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,48 +95,14 @@ class _SDAMStatusMessage(str, enum.Enum):
9595
ConnectionClosedReason.IDLE: "Connection was idle too long",
9696
ConnectionCheckOutFailedReason.TIMEOUT: "Connection exceeded the specified timeout",
9797
}
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-
with self.lock:
118-
self.buffer.clear()
119-
self.capacity = self.original_capacity
120-
12198

12299
if os.environ.get("DEBUG_LOG"):
123-
FORMAT = "%(levelname)s %(name)s %(message)s"
124-
handler = logging.StreamHandler()
125-
handler.setFormatter(logging.Formatter(FORMAT))
126-
_DEBUG_LOG_HANDLER = LoggingHandler(target=handler)
127-
128100
_COMMAND_LOGGER.setLevel(logging.DEBUG)
129101
_SERVER_SELECTION_LOGGER.setLevel(logging.DEBUG)
130102
_CONNECTION_LOGGER.setLevel(logging.DEBUG)
131103
_CLIENT_LOGGER.setLevel(logging.DEBUG)
132104
_SDAM_LOGGER.setLevel(logging.DEBUG)
133105

134-
_COMMAND_LOGGER.addHandler(_DEBUG_LOG_HANDLER)
135-
_SERVER_SELECTION_LOGGER.addHandler(_DEBUG_LOG_HANDLER)
136-
_CONNECTION_LOGGER.addHandler(_DEBUG_LOG_HANDLER)
137-
_CLIENT_LOGGER.addHandler(_DEBUG_LOG_HANDLER)
138-
_SDAM_LOGGER.addHandler(_DEBUG_LOG_HANDLER)
139-
140106

141107
def _log_client_error() -> None:
142108
# This is called from a daemon thread so check for None to account for interpreter shutdown.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ minversion = "7"
9898
addopts = ["-ra", "--strict-config", "--strict-markers", "--junitxml=xunit-results/TEST-results.xml", "-m default or default_async"]
9999
testpaths = ["test"]
100100
log_cli_level = "INFO"
101+
log_cli = true
101102
faulthandler_timeout = 1500
102103
asyncio_default_fixture_loop_scope = "session"
103104
xfail_strict = true

test/asynchronous/conftest.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,9 @@
88
import pytest
99
import pytest_asyncio
1010

11-
from pymongo.logger import _DEBUG_LOG_HANDLER
12-
1311
_IS_SYNC = False
1412

1513

16-
@pytest.hookimpl(hookwrapper=True)
17-
def pytest_runtest_makereport(item):
18-
if _DEBUG_LOG_HANDLER is not None:
19-
outcome = yield
20-
rep = outcome.get_result()
21-
if rep.failed:
22-
_DEBUG_LOG_HANDLER.flush()
23-
else:
24-
_DEBUG_LOG_HANDLER.clear()
25-
else:
26-
yield
27-
28-
2914
@pytest.fixture(scope="session")
3015
def event_loop_policy():
3116
# The default asyncio loop implementation on Windows

test/conftest.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
import pytest
88

9-
from pymongo.logger import _DEBUG_LOG_HANDLER
10-
119
_IS_SYNC = True
1210

1311

14-
@pytest.hookimpl(hookwrapper=True)
15-
def pytest_runtest_makereport(item):
16-
if _DEBUG_LOG_HANDLER is not None:
17-
outcome = yield
18-
rep = outcome.get_result()
19-
if rep.failed:
20-
_DEBUG_LOG_HANDLER.flush()
21-
else:
22-
_DEBUG_LOG_HANDLER.clear()
23-
else:
24-
yield
25-
26-
2712
@pytest.fixture(scope="session")
2813
def event_loop_policy():
2914
# The default asyncio loop implementation on Windows

0 commit comments

Comments
 (0)