Skip to content

Commit a558136

Browse files
committed
preserve contextvars if necessary - closes #20
1 parent 5b67a47 commit a558136

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: tests
22

33
on:
44
push:
5-
branches: ["master"]
5+
branches: ["main"]
66
pull_request:
7-
branches: ["master"]
7+
branches: ["main"]
88
workflow_dispatch:
99

1010
jobs:
@@ -14,17 +14,16 @@ jobs:
1414

1515
strategy:
1616
matrix:
17-
python-version: ["2.7", "3.10"]
17+
python-version: ["2.7", "3.11"]
1818

1919
steps:
20-
- uses: "actions/checkout@v2"
21-
- uses: "actions/setup-python@v2"
20+
- uses: "actions/checkout@v3"
21+
- uses: "actions/setup-python@v4"
2222
with:
2323
python-version: "${{ matrix.python-version }}"
2424
- name: "Install"
2525
run: |
2626
set -xe
27-
python -VV
2827
python -m pip install --editable .
2928
- name: "Run tests for ${{ matrix.python-version }}"
3029
run: python -m pytest

pytest_structlog.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
import pytest
33
import structlog
44

5+
try:
6+
from structlog.contextvars import merge_contextvars
7+
except ImportError:
8+
# structolg < 20.1.0
9+
# use a "missing" sentinel to avoid a NameError later on
10+
merge_contextvars = object()
511

6-
__version__ = "0.5"
12+
13+
__version__ = "0.6"
714

815

916
class EventList(list):
@@ -73,14 +80,17 @@ def log(monkeypatch, request):
7380

7481
# redirect logging to log capture
7582
cap = StructuredLogCapture()
83+
new_processors = []
7684
for processor in original_processors:
7785
if isinstance(processor, structlog.stdlib.PositionalArgumentsFormatter):
7886
# if there was a positional argument formatter in there, keep it there
7987
# see https://github.com/wimglenn/pytest-structlog/issues/18
80-
new_processors = [processor, cap.process]
81-
break
82-
else:
83-
new_processors = [cap.process]
88+
new_processors.append(processor)
89+
elif processor is merge_contextvars:
90+
# if merging contextvars, preserve
91+
# see https://github.com/wimglenn/pytest-structlog/issues/20
92+
new_processors.append(processor)
93+
new_processors.append(cap.process)
8494
structlog.configure(processors=new_processors, cache_logger_on_first_use=False)
8595
cap.original_configure = configure = structlog.configure
8696
cap.configure_once = structlog.configure_once

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="pytest-structlog",
5-
version="0.5",
5+
version="0.6",
66
url="https://github.com/wimglenn/pytest-structlog",
77
description="Structured logging assertions",
88
long_description=open("README.rst").read(),

tests/test_issue18.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def stdlib_configure():
2121
],
2222
logger_factory=structlog.stdlib.LoggerFactory(),
2323
wrapper_class=structlog.stdlib.BoundLogger,
24-
context_class=structlog.threadlocal.wrap_dict(dict),
2524
)
2625

2726

tests/test_issue20.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
import structlog
3+
4+
5+
logger = structlog.get_logger()
6+
7+
8+
@pytest.fixture
9+
def issue20_setup():
10+
structlog.configure(
11+
processors=[
12+
structlog.contextvars.merge_contextvars,
13+
structlog.stdlib.filter_by_level,
14+
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
15+
],
16+
wrapper_class=structlog.stdlib.BoundLogger,
17+
logger_factory=structlog.stdlib.LoggerFactory(),
18+
context_class=dict,
19+
)
20+
yield
21+
structlog.contextvars.clear_contextvars()
22+
23+
24+
def test_contextvar(issue20_setup, log):
25+
structlog.contextvars.clear_contextvars()
26+
logger.info("log1", log1var="value")
27+
structlog.contextvars.bind_contextvars(contextvar="cv")
28+
logger.info("log2", log2var="value")
29+
assert log.has("log2", log2var="value", contextvar="cv")

0 commit comments

Comments
 (0)