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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibcpy 3.10–3.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.
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.
fixRun 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`.
fixIn 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.
fixEnsure 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.