pytest-logger
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.
Common errors
-
Logs are not showing up in the console during test execution.
cause 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()`.
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`.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.
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.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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha 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.
Install
-
pip install pytest-logger
Imports
- pytest_logger_config
# Defined in conftest.py def pytest_logger_config(logger_config):
- logging
import logging
Quickstart
# 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.