{"id":7615,"library":"pytest-loguru","title":"Pytest Loguru","description":"pytest-loguru is a pytest plugin that integrates Loguru with pytest's `caplog` fixture. It replaces the default `caplog` fixture to seamlessly capture log messages emitted by Loguru, allowing for easy assertion in tests. The current version is 0.4.0, and it maintains a moderate release cadence with updates addressing compatibility and minor improvements.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/mcarans/pytest-loguru","tags":["pytest","loguru","logging","testing","plugin"],"install":[{"cmd":"pip install pytest-loguru","lang":"bash","label":"Install plugin"}],"dependencies":[{"reason":"This is a pytest plugin and requires pytest to function.","package":"pytest","optional":false},{"reason":"This plugin integrates with Loguru and requires it to be installed to capture Loguru messages.","package":"loguru","optional":false}],"imports":[],"quickstart":{"code":"from loguru import logger\nimport pytest\n\ndef test_loguru_messages_captured(caplog):\n    logger.info(\"This is an info message from Loguru\")\n    logger.warning(\"This is a warning message from Loguru\")\n    assert \"This is an info message from Loguru\" in caplog.text\n    assert \"This is a warning message from Loguru\" in caplog.text\n    assert caplog.records[0].levelname == \"INFO\"\n    assert caplog.records[1].levelname == \"WARNING\"","lang":"python","description":"This quickstart demonstrates how to use the `caplog` fixture to capture messages emitted by Loguru. After installing `pytest-loguru`, the `caplog` fixture automatically intercepts Loguru messages, making them available for assertions via `caplog.text` and `caplog.records`."},"warnings":[{"fix":"Ensure your tests are designed for Loguru's logging mechanisms when `pytest-loguru` is active. For `logging` module specific tests, consider running them in an environment without `pytest-loguru` or using Loguru's configuration options to achieve similar effects.","message":"The `pytest-loguru` plugin replaces pytest's default `caplog` fixture. If you have tests that rely on specific behaviors of the standard `caplog` for `logging` module messages, or use `caplog.set_level()`, you might encounter unexpected behavior as `pytest-loguru` intercepts Loguru's output instead.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To prevent duplicate output, you can use `logger.remove()` and `logger.add()` within your test setup (e.g., a fixture) to control Loguru's handlers, ensuring only the necessary ones (which `pytest-loguru` can intercept) are active during the test.","message":"Loguru, by default, outputs to `sys.stderr`. While `pytest-loguru` captures this for `caplog`, you might still see duplicate output on the console during test runs if Loguru's handlers are not managed correctly within your tests or if multiple handlers are configured for the logger.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Configure Loguru using its native API (e.g., `logger.remove()`, `logger.add(sys.stderr, level='WARNING')`) to control logging levels and destinations within your tests.","message":"Attempting to use standard `logging` module methods like `caplog.set_level()` or `logger.propagate = False` directly on the `loguru.logger` object will not work, as Loguru's API is different from the standard `logging` module.","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":"Verify `pytest-loguru` is installed (`pip install pytest-loguru`). Ensure Loguru has at least one handler configured (e.g., the default `sys.stderr` handler) that the plugin can intercept. Avoid `logger.remove()` without a subsequent `logger.add()` that directs output to a stream `pytest-loguru` monitors.","cause":"Loguru messages are not being captured by `caplog`. This usually happens if `pytest-loguru` is not installed, not enabled, or Loguru's handlers are configured in a way that prevents interception.","error":"AssertionError: 'Your Loguru message' not in caplog.text"},{"fix":"Confirm `pytest-loguru` is correctly installed. The plugin primarily intercepts messages sent to `sys.stderr`. If you've redirected Loguru's output to files or custom handlers, `caplog` may not see them. Ensure a handler writing to `sys.stderr` is active or configure Loguru to log to a stream that can be captured.","cause":"Similar to the above, the `pytest-loguru` plugin might not be active, or Loguru is configured to send logs to handlers that `pytest-loguru` does not monitor.","error":"Log messages appear in the console/stderr during tests, but `caplog.text` is empty."},{"fix":"Use Loguru's own methods for configuration. For example, to set the level, use `logger.remove()` and `logger.add(sys.stderr, level='INFO')` or configure handlers explicitly.","cause":"This error occurs when trying to use `logging` module methods (like `setLevel`) directly on a `loguru.logger` instance. Loguru's API for configuration is distinct from the standard `logging` module.","error":"TypeError: 'LoguruLogger' object has no attribute 'setLevel'"}]}