pytest-dev / pytest-dev/pytest

Can't access "Captured stderr call" section of logging

Open
#5,160 4 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: logging
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

  • Include a detailed description of the bug or suggestion

I'm attempting to capture the output of a custom logging configuration dict. But for some reason the once I use logging.config.dictConfig all the logging output is pushed into the Captured stderr call and I can't see it in the caplog fixture.

How do I access this logging data?

  • pip list of the virtual environment you are using
Package        Version  Location
-------------- -------- -------------------
aiohttp        3.3.2
aioredis       1.2.0
async-timeout  3.0.1
atomicwrites   1.3.0
attrs          19.1.0
certifi        2019.3.9
chardet        3.0.4
coverage       4.5.3
datadog        0.21.0
decorator      4.4.0
flake8         3.5.0
hiredis        1.0.0
idna           2.6
idna-ssl       1.1.0
mccabe         0.6.1
more-itertools 7.0.0
multidict      4.5.2
pip            10.0.1
pluggy         0.6.0
py             1.8.0
pyaml          17.12.1
pycodestyle    2.3.1
pyflakes       1.6.0
pytest         3.6.3
pytest-aiohttp 0.3.0
pytest-asyncio 0.5.0
pytest-cov     2.5.1
pytest-html    1.13.0
pytz           2018.4
PyYAML         5.1
redis          3.2.1
requests       2.18.4
setuptools     39.2.0
simplejson     3.16.0
six            1.12.0
urllib3        1.22
wheel          0.31.1
yarl           1.3.0
  • pytest and operating system versions
pytest         3.6.3
docker Desktop Mac Version 2.0.0.3 (31259) running python:3.6.5-slim container
  • Minimal example if possible
import logging
import logging.config
import pytest

logger = logging.getLogger(__name__)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'console': {
            'datefmt': "%Y-%m-%dT%H:%M:%S",
            'format': "%(asctime)s.%(msecs)d %(levelname)s %(processName)s:%(threadName)s %(name)s %(message)s",
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'console',
            'level': 'DEBUG',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True
        }
    },
}

logging.config.dictConfig(LOGGING)

def test_logging_time_formatting(caplog):
    caplog.set_level(logging.DEBUG)

    logger.info("testing")

    print(caplog.record_tuples)

    assert "INFO MainProcess:MainThread test_logging testing" in caplog.text

Running this test gives me this output (notice the INFO log in the Captured stderr call section):

docker-compose run --rm utils bash -c 'pytest -v tests/test_logging.py'
=========================================================================================================================================================================================================== test session starts ============================================================================================================================================================================================================
platform linux -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- /usr/local/bin/python3
cachedir: .pytest_cache
rootdir: /code/utils, inifile:
plugins: html-1.13.0, cov-2.5.1, asyncio-0.5.0, aiohttp-0.3.0
collected 1 item

tests/test_logging.py::test_logging_time_formatting FAILED                                                                                                                                                                                                                                                                                                                                                                           [100%]

================================================================================================================================================================================================================= FAILURES =================================================================================================================================================================================================================
_______________________________________________________________________________________________________________________________________________________________________________________________________ test_logging_time_formatting _______________________________________________________________________________________________________________________________________________________________________________________________________

caplog = <_pytest.logging.LogCaptureFixture object at 0x7f7689814f60>

    def test_logging_time_formatting(caplog):
        caplog.set_level(logging.DEBUG)

        logger.info("testing")

        print(caplog.record_tuples)

>       assert "INFO MainProcess:MainThread test_logging testing" in caplog.text
E       AssertionError: assert 'INFO MainProcess:MainThread test_logging testing' in 'test_logging.py             43 INFO     testing\n'
E        +  where 'test_logging.py             43 INFO     testing\n' = <_pytest.logging.LogCaptureFixture object at 0x7f7689814f60>.text

tests/test_logging.py:51: AssertionError
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Captured stdout call -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[('test_logging', 20, 'testing')]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Captured stderr call -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2019-04-23T19:54:59.397 INFO MainProcess:MainThread test_logging testing
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Captured log call -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_logging.py             43 INFO     testing
========================================================================================================================================================================================================= 1 failed in 0.11 seconds =========================================================================================================================================================================================================
make: *** [test] Error 1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the minimal example with pytest's caplog fixture and Python's logging.config.dictConfig. Trace how configured logging handlers interact with log capture, then determine the expected access to the captured records. Done means the behavior is covered by a regression test or the issue's expected usage is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.