File tree Expand file tree Collapse file tree 5 files changed +51
-13
lines changed
Expand file tree Collapse file tree 5 files changed +51
-13
lines changed Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ name: tests
22
33on :
44 push :
5- branches : ["master "]
5+ branches : ["main "]
66 pull_request :
7- branches : ["master "]
7+ branches : ["main "]
88 workflow_dispatch :
99
1010jobs :
@@ -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
Original file line number Diff line number Diff line change 22import pytest
33import 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
916class 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
Original file line number Diff line number Diff line change 22
33setup (
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 (),
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ import pytest
2+ import structlog
3+
4+
5+ logger = structlog .get_logger ()
6+
7+
8+ @pytest .fixture
9+ def issue20_setup ():
10+ pytest .importorskip ("structlog.contextvars" )
11+ structlog .configure (
12+ processors = [
13+ structlog .contextvars .merge_contextvars ,
14+ structlog .stdlib .filter_by_level ,
15+ structlog .stdlib .ProcessorFormatter .wrap_for_formatter ,
16+ ],
17+ wrapper_class = structlog .stdlib .BoundLogger ,
18+ logger_factory = structlog .stdlib .LoggerFactory (),
19+ context_class = dict ,
20+ )
21+ yield
22+ structlog .contextvars .clear_contextvars ()
23+
24+
25+ def test_contextvar (issue20_setup , log ):
26+ structlog .contextvars .clear_contextvars ()
27+ logger .info ("log1" , log1var = "value" )
28+ structlog .contextvars .bind_contextvars (contextvar = "cv" )
29+ logger .info ("log2" , log2var = "value" )
30+ assert log .has ("log2" , log2var = "value" , contextvar = "cv" )
You can’t perform that action at this time.
0 commit comments