Skip to content

Commit

Permalink
Merge pull request #21 from wimglenn/contextvars
Browse files Browse the repository at this point in the history
preserve contextvars if necessary - closes #20
  • Loading branch information
wimglenn authored Dec 18, 2022
2 parents 5b67a47 + 6647429 commit feb680c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: tests

on:
push:
branches: ["master"]
branches: ["main"]
pull_request:
branches: ["master"]
branches: ["main"]
workflow_dispatch:

jobs:
Expand All @@ -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
20 changes: 15 additions & 5 deletions pytest_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion tests/test_issue18.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def stdlib_configure():
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
context_class=structlog.threadlocal.wrap_dict(dict),
)


Expand Down
30 changes: 30 additions & 0 deletions tests/test_issue20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import structlog


logger = structlog.get_logger()


@pytest.fixture
def issue20_setup():
pytest.importorskip("structlog.contextvars")
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")

0 comments on commit feb680c

Please sign in to comment.