Mayil-AI / loguru

MIT License
0 stars 0 forks source link

Difficulty in Configuring Loguru to Honor --log-cli-level=INFO in Pytest (hashtag1062) #11

Open vikramsubramanian opened 3 months ago

vikramsubramanian commented 3 months ago

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 that DEBUG 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

  1. Write a test function that includes loguru logger messages at various levels, including DEBUG.
  2. Run Pytest with the command: python -m pytest -s tests --log-cli-level=INFO.
  3. Observe that DEBUG messages are still appearing in the output.

hashtaghashtaghashtag What I've Tried

hashtaghashtaghashtag Expected Behavior I would expect that running Pytest with --log-cli-level=INFO would result in DEBUG 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. )

mayil-ai[bot] commented 3 months ago

Possible Solution

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)

Code snippets to check

loguru → _logger.py This snippet contains the 'add' method which is used to configure Loguru sinks, including setting the level of logging. It is relevant to the issue as the user is having trouble with setting the correct log level when using Pytest. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_logger.py#L253-L2094 This snippet includes code that checks the log level and determines whether to proceed with logging. It is relevant because it may contain logic that needs to be adjusted to respect the '--log-cli-level' flag from Pytest. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_logger.py#L1906-L2020 This snippet defines the logging methods for different levels, including 'DEBUG' and 'INFO'. It is relevant because the issue involves these specific log levels and their behavior when running tests with Pytest. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_logger.py#L2033-L2086