Open vikramsubramanian opened 3 months ago
DEBUG
level messages are not displayed when --log-cli-level=INFO
is used, you need to configure the Loguru logger to respect the Pytest logging level.configure_logger
function to set the Loguru logger level based on the Pytest --log-cli-level
flag.caplog
fixture to capture the logging output and set the appropriate level.Here is a solution to configure Loguru correctly with Pytest:
# Pytest logging integration
import pytest
from loguru import logger
@pytest.fixture
def caplog(caplog):
class PropagateHandler(logging.Handler):
def emit(self, record):
logging.getLogger(record.name).handle(record)
handler_id = logger.add(PropagateHandler(), format="{message}")
logger.level("DEBUG", no=10, color="<blue>", icon="🐞")
logger.level("INFO", no=20, color="<green>", icon="ℹ️")
logger.level("WARNING", no=30, color="<yellow>", icon="⚠️")
logger.level("ERROR", no=40, color="<red>", icon="❌")
logger.level("CRITICAL", no=50, color="<RED><bold>", icon="☠️")
caplog.set_level(logging.INFO)
yield caplog
logger.remove(handler_id)
PropagateHandler
class inside the caplog
fixture to propagate logs from Loguru to the standard logging module.logger.add
to add the PropagateHandler
to Loguru's handlers.no
(number) matches the standard logging levels.caplog
level to logging.INFO
or retrieve the level from the Pytest configuration if you want to dynamically set it based on the --log-cli-level
flag.caplog
for the test, remove the handler from Loguru to clean up.
hashtaghashtaghashtag Environment
hashtaghashtaghashtag Description I am encountering an issue with configuring Loguru to work in tandem with Pytest's
--log-cli-level
flag. Recently I start to switch logging from python native logging to Loguru. It's indeed simple and coll. However, I found when it goes to pytest, it became somehow confusing. My goal is to run Pytest with the flag--log-cli-level=INFO
or--log-level=INFO
and ensure thatDEBUG
level messages are not shown in the test output. However, despite various attempts and configurations,DEBUG
messages are still being displayed.hashtaghashtaghashtag Steps to Reproduce
loguru
logger messages at various levels, includingDEBUG
.python -m pytest -s tests --log-cli-level=INFO
.DEBUG
messages are still appearing in the output.hashtaghashtaghashtag What I've Tried
I've read through related GitHub issues (GHhashtag59 and GHhashtag474) and the official
loguru
documentation (I've tried using a
caplog
fixture to integrateloguru
logger withpytest
's logging mechanism as shown below:I've also tried adding a
reportlog
fixture to handle log messages:I've also try
pytest-loguru
, but also not getting what I want.hashtaghashtaghashtag Expected Behavior I would expect that running Pytest with
--log-cli-level=INFO
would result inDEBUG
level messages being filtered out from the test output.hashtaghashtaghashtag Actual Behavior
DEBUG
level messages are still being displayed in the test output.hashtaghashtaghashtag Request Could you please provide guidance or a solution on how to configure
loguru
to respect the--log-cli-level=INFO
flag in Pytest? Any assistance or insights into this issue would be greatly appreciated.Thank you for your time and help. )