From a5581360f9c1018b1dc398524a86952273a28aed Mon Sep 17 00:00:00 2001 From: wim glenn Date: Sat, 17 Dec 2022 19:49:11 -0600 Subject: [PATCH 1/2] preserve contextvars if necessary - closes #20 --- .github/workflows/tests.yml | 11 +++++------ pytest_structlog.py | 20 +++++++++++++++----- setup.py | 2 +- tests/test_issue18.py | 1 - tests/test_issue20.py | 29 +++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 tests/test_issue20.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 87ccfc0..47640ab 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,9 +2,9 @@ name: tests on: push: - branches: ["master"] + branches: ["main"] pull_request: - branches: ["master"] + branches: ["main"] workflow_dispatch: jobs: @@ -14,17 +14,16 @@ jobs: strategy: matrix: - python-version: ["2.7", "3.10"] + python-version: ["2.7", "3.11"] steps: - - uses: "actions/checkout@v2" - - uses: "actions/setup-python@v2" + - uses: "actions/checkout@v3" + - uses: "actions/setup-python@v4" with: python-version: "${{ matrix.python-version }}" - name: "Install" run: | set -xe - python -VV python -m pip install --editable . - name: "Run tests for ${{ matrix.python-version }}" run: python -m pytest diff --git a/pytest_structlog.py b/pytest_structlog.py index 22a1b0e..c8924e5 100644 --- a/pytest_structlog.py +++ b/pytest_structlog.py @@ -2,8 +2,15 @@ import pytest import structlog +try: + from structlog.contextvars import merge_contextvars +except ImportError: + # structolg < 20.1.0 + # use a "missing" sentinel to avoid a NameError later on + merge_contextvars = object() -__version__ = "0.5" + +__version__ = "0.6" class EventList(list): @@ -73,14 +80,17 @@ def log(monkeypatch, request): # redirect logging to log capture cap = StructuredLogCapture() + new_processors = [] for processor in original_processors: if isinstance(processor, structlog.stdlib.PositionalArgumentsFormatter): # if there was a positional argument formatter in there, keep it there # see https://github.com/wimglenn/pytest-structlog/issues/18 - new_processors = [processor, cap.process] - break - else: - new_processors = [cap.process] + new_processors.append(processor) + elif processor is merge_contextvars: + # if merging contextvars, preserve + # see https://github.com/wimglenn/pytest-structlog/issues/20 + new_processors.append(processor) + new_processors.append(cap.process) structlog.configure(processors=new_processors, cache_logger_on_first_use=False) cap.original_configure = configure = structlog.configure cap.configure_once = structlog.configure_once diff --git a/setup.py b/setup.py index 8aac691..5e927d2 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="pytest-structlog", - version="0.5", + version="0.6", url="https://github.com/wimglenn/pytest-structlog", description="Structured logging assertions", long_description=open("README.rst").read(), diff --git a/tests/test_issue18.py b/tests/test_issue18.py index cabb410..40b2cfc 100644 --- a/tests/test_issue18.py +++ b/tests/test_issue18.py @@ -21,7 +21,6 @@ def stdlib_configure(): ], logger_factory=structlog.stdlib.LoggerFactory(), wrapper_class=structlog.stdlib.BoundLogger, - context_class=structlog.threadlocal.wrap_dict(dict), ) diff --git a/tests/test_issue20.py b/tests/test_issue20.py new file mode 100644 index 0000000..23f3da7 --- /dev/null +++ b/tests/test_issue20.py @@ -0,0 +1,29 @@ +import pytest +import structlog + + +logger = structlog.get_logger() + + +@pytest.fixture +def issue20_setup(): + structlog.configure( + processors=[ + structlog.contextvars.merge_contextvars, + structlog.stdlib.filter_by_level, + structlog.stdlib.ProcessorFormatter.wrap_for_formatter, + ], + wrapper_class=structlog.stdlib.BoundLogger, + logger_factory=structlog.stdlib.LoggerFactory(), + context_class=dict, + ) + yield + structlog.contextvars.clear_contextvars() + + +def test_contextvar(issue20_setup, log): + structlog.contextvars.clear_contextvars() + logger.info("log1", log1var="value") + structlog.contextvars.bind_contextvars(contextvar="cv") + logger.info("log2", log2var="value") + assert log.has("log2", log2var="value", contextvar="cv") From 6647429aafbbcf9d2f3615c4dc298d81103aa2f8 Mon Sep 17 00:00:00 2001 From: wim glenn Date: Sat, 17 Dec 2022 20:17:16 -0600 Subject: [PATCH 2/2] compat --- tests/test_issue20.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_issue20.py b/tests/test_issue20.py index 23f3da7..81c5e61 100644 --- a/tests/test_issue20.py +++ b/tests/test_issue20.py @@ -7,6 +7,7 @@ @pytest.fixture def issue20_setup(): + pytest.importorskip("structlog.contextvars") structlog.configure( processors=[ structlog.contextvars.merge_contextvars,