diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..5c58d32 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,30 @@ +name: tests + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + workflow_dispatch: + +jobs: + tests: + name: "Python ${{ matrix.python-version }}" + runs-on: "ubuntu-latest" + + strategy: + matrix: + python-version: ["2.7", "3.9"] + + steps: + - uses: "actions/checkout@v2" + - uses: "actions/setup-python@v2" + 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/.travis.yml b/.travis.yml deleted file mode 100644 index 6398201..0000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: python - -sudo: false - -python: - - "2.7" - - "3.6" - - "3.7" - - "3.8" - - "3.9" - - "nightly" - -matrix: - fast_finish: true - allow_failures: - - python: "nightly" - -install: - - pip install --upgrade pytest - - pip install --editable . - -script: - - pytest - -notifications: - email: false diff --git a/README.rst b/README.rst index 0c5663f..c9be9fd 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ -|travis|_ |pypi|_ |pyversions|_ |womm|_ +|actions|_ |pypi|_ |pyversions|_ |womm|_ -.. |travis| image:: https://travis-ci.com/wimglenn/pytest-structlog.svg?branch=master -.. _travis: https://travis-ci.com/wimglenn/pytest-structlog +.. |actions| image:: https://github.com/wimglenn/pytest-structlog/actions/workflows/tests.yml/badge.svg +.. _actions: https://github.com/wimglenn/pytest-structlog/actions/workflows/tests.yml/ .. |pypi| image:: https://img.shields.io/pypi/v/pytest-structlog.svg .. _pypi: https://pypi.org/project/pytest-structlog diff --git a/pytest_structlog.py b/pytest_structlog.py index d6ad40a..22a1b0e 100644 --- a/pytest_structlog.py +++ b/pytest_structlog.py @@ -3,7 +3,7 @@ import structlog -__version__ = "0.4" +__version__ = "0.5" class EventList(list): @@ -69,19 +69,28 @@ def log(monkeypatch, request): Example usage: ``assert log.has("some message", var1="extra context")`` """ # save settings for later - processors = structlog.get_config().get("processors", []) - configure = structlog.configure + original_processors = structlog.get_config().get("processors", []) # redirect logging to log capture cap = StructuredLogCapture() - structlog.configure(processors=[cap.process], cache_logger_on_first_use=False) + 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] + structlog.configure(processors=new_processors, cache_logger_on_first_use=False) + cap.original_configure = configure = structlog.configure + cap.configure_once = structlog.configure_once monkeypatch.setattr("structlog.configure", no_op) monkeypatch.setattr("structlog.configure_once", no_op) request.node.structlog_events = cap.events yield cap - # back to normal behavior - configure(processors=processors) + # back to original behavior + configure(processors=original_processors) @pytest.hookimpl(tryfirst=True, hookwrapper=True) diff --git a/setup.py b/setup.py index 5e65659..8aac691 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="pytest-structlog", - version="0.4", + version="0.5", 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 new file mode 100644 index 0000000..cabb410 --- /dev/null +++ b/tests/test_issue18.py @@ -0,0 +1,32 @@ +import pytest + +import structlog + + +logger = structlog.get_logger(__name__) + + +@pytest.fixture +def stdlib_configure(): + structlog.configure( + processors=[ + structlog.stdlib.filter_by_level, + structlog.stdlib.add_logger_name, + structlog.stdlib.add_log_level, + structlog.stdlib.PositionalArgumentsFormatter(), + structlog.processors.TimeStamper(fmt="iso"), + structlog.processors.StackInfoRenderer(), + structlog.processors.format_exc_info, + structlog.dev.ConsoleRenderer(), + ], + logger_factory=structlog.stdlib.LoggerFactory(), + wrapper_class=structlog.stdlib.BoundLogger, + context_class=structlog.threadlocal.wrap_dict(dict), + ) + + +def test_positional_formatting(stdlib_configure, log): + items_count = 2 + dt = 0.02 + logger.info("Processed %d CC items in total in %.2f seconds", items_count, dt) + assert log.has("Processed 2 CC items in total in 0.02 seconds", level="info")