Registry / testing / pytest-logger

pytest-logger

JSON →
library1.1.1pypypi✓ verified 87d ago

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.

pip install pytest-logger
INSTALL
IMPORT
SIG · PYTEST-LOGGER
P
pytest-logger
testingpythonv1.1.1
Install
2.7s avg
Import
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.7s · import 0.000s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest_logger_config
# Defined in conftest.py def pytest_logger_config(logger_config):
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.
logging
import logging
Standard Python logging module used within tests, configured by pytest-logger.

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`.

# content of conftest.py import os import logging def pytest_logger_config(logger_config): logger_config.add_loggers(['foo', 'bar'], stdout_level='info', file_level='debug') logger_config.set_log_option_default('foo,bar') def pytest_logger_logdirlink(config): return os.path.join(os.path.dirname(__file__), 'logs') # content of test_example.py import logging foo_logger = logging.getLogger('foo') bar_logger = logging.getLogger('bar') baz_logger = logging.getLogger('baz') def test_logging_output(): foo_logger.info('This is an INFO message from foo') bar_logger.debug('This is a DEBUG message from bar, only in file') baz_logger.warning('This is a WARNING from baz, not configured by default') assert True # To run: pytest -s -v --log-cli-level=info --loggers=foo,bar test_example.py # This will output foo INFO messages to console and foo (INFO) and bar (DEBUG) to files in the 'logs' directory.
Debug
Known issues
gotchaTo 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.
fix
Run pytest with `pytest -s` or `pytest --capture=no`.
affects: All versions
gotchaBy 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.
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.
affects: All versions
gotchaWhen 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.
fix
If real-time terminal feedback is critical, avoid `pytest-xdist` or rely on file logging for detailed analysis during parallel runs.
affects: All versions
Errors
Common errors & fixes
Logs are not showing up in the console during test execution.
Pytest captures stdout/stderr by default, preventing real-time log display.
fix
Run pytest with the `-s` flag (e.g., `pytest -s`) or `--capture=no`.
DEBUG or INFO level messages are not appearing in console/files, even after using `logging.getLogger()`.
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`.
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.
Log files are either not created or are empty in the specified log directory.
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.
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.
Upgrade
Version history
1.1.1latest on PyPI · released Mar 10, 2024
Audit
Dependencies
pytestrequiredCore testing framework; pytest-logger is a plugin for it.
Agent activity
27 hits · last 30 days
node
22
OpenAI (training)
2
Resources
pytest-logger — pip install pytest-logger · libregistry