{"id":8536,"library":"pytest-logger","title":"pytest-logger","description":"pytest-logger is a pytest plugin that configures handlers for loggers from Python's standard `logging` module. It allows directing logs to the terminal or files in a configurable manner. The current version is 1.1.1, released on March 10, 2024, indicating active maintenance and an irregular release cadence.","status":"active","version":"1.1.1","language":"en","source_language":"en","source_url":"https://github.com/aurzenligl/pytest-logger","tags":["pytest-plugin","logging","testing","devops"],"install":[{"cmd":"pip install pytest-logger","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core testing framework; pytest-logger is a plugin for it.","package":"pytest","optional":false}],"imports":[{"note":"pytest-logger primarily operates through pytest hooks defined in conftest.py, rather than direct imports into test files. The main configuration entry point is the `pytest_logger_config` hook.","symbol":"pytest_logger_config","correct":"# Defined in conftest.py\ndef pytest_logger_config(logger_config):"},{"note":"Standard Python logging module used within tests, configured by pytest-logger.","symbol":"logging","correct":"import logging"}],"quickstart":{"code":"# content of conftest.py\nimport os\nimport logging\n\ndef pytest_logger_config(logger_config):\n    logger_config.add_loggers(['foo', 'bar'], stdout_level='info', file_level='debug')\n    logger_config.set_log_option_default('foo,bar')\n\ndef pytest_logger_logdirlink(config):\n    return os.path.join(os.path.dirname(__file__), 'logs')\n\n# content of test_example.py\nimport logging\n\nfoo_logger = logging.getLogger('foo')\nbar_logger = logging.getLogger('bar')\nbaz_logger = logging.getLogger('baz')\n\ndef test_logging_output():\n    foo_logger.info('This is an INFO message from foo')\n    bar_logger.debug('This is a DEBUG message from bar, only in file')\n    baz_logger.warning('This is a WARNING from baz, not configured by default')\n    assert True\n\n# To run: pytest -s -v --log-cli-level=info --loggers=foo,bar test_example.py\n# This will output foo INFO messages to console and foo (INFO) and bar (DEBUG) to files in the 'logs' directory.","lang":"python","description":"To quickly get started, define `pytest_logger_config` and `pytest_logger_logdirlink` hooks in your `conftest.py` to set up loggers and a directory for log files. Then, use standard Python's `logging` module within your test files. Run pytest with `-s` to see real-time console output, and specify which loggers to display with `--loggers` and their console level with `--log-cli-level`."},"warnings":[{"fix":"Run pytest with `pytest -s` or `pytest --capture=no`.","message":"To see logs printed to the terminal in real-time during test execution, you must disable output capturing by passing the `-s` or `--capture=no` switch to pytest. Otherwise, logs will be captured and only shown upon test failure.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly set `stdout_level` and `file_level` in `logger_config.add_loggers()` for all desired loggers. Consider adding a `NullHandler` to unconfigured loggers if you encounter 'no handlers' warnings.","message":"By default, Python's root logger (and its children) has a `WARNING` level threshold. If you configure any logger via `pytest-logger`, the root logger's level is set to `NOTSET` to allow all logs through. Be mindful of this default if you expect `DEBUG` or `INFO` messages from unconfigured loggers and they don't appear. Explicitly set `stdout_level` and `file_level` for desired loggers.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If real-time terminal feedback is critical, avoid `pytest-xdist` or rely on file logging for detailed analysis during parallel runs.","message":"When using `pytest-xdist` for parallel test execution, stdout output from `pytest-logger` is not printed to the terminal. However, log file output will work as expected in single-process mode.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run pytest with the `-s` flag (e.g., `pytest -s`) or `--capture=no`.","cause":"Pytest captures stdout/stderr by default, preventing real-time log display.","error":"Logs are not showing up in the console during test execution."},{"fix":"In your `conftest.py`, ensure `logger_config.add_loggers()` sets `stdout_level='debug'` and/or `file_level='debug'` for the relevant logger names. Also, check that you are running with `--log-cli-level=debug` or similar if expecting console output.","cause":"The default root logger level might be too high (e.g., WARNING), or the `stdout_level`/`file_level` for the specific logger was not set to a sufficiently low level (e.g., 'debug' or 'info') in `pytest_logger_config`.","error":"DEBUG or INFO level messages are not appearing in console/files, even after using `logging.getLogger()`."},{"fix":"Ensure that `logger_config.add_loggers()` specifies a `file_level` (e.g., `file_level='debug'`). Verify that the `pytest_logger_logdirlink` hook in `conftest.py` returns a valid, writable path, and that a 'logs' directory exists or can be created relative to your tests.","cause":"File logging is not explicitly enabled or configured for the loggers, or the `pytest_logger_logdirlink` hook is not correctly implemented/pointing to a writable location.","error":"Log files are either not created or are empty in the specified log directory."}]}