This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import logging
|
||||
import pytest
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
from slic.utils.logign import *
|
||||
|
||||
import logzero
|
||||
from logzero import logger as log
|
||||
|
||||
|
||||
# Setup custom levels for testing
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def setup_logger():
|
||||
add_log_Level(log, "ENLARGE", logging.INFO + 5, func_name="enlarge")
|
||||
logcfg("DEBUG")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("level,msg,should_appear", [
|
||||
("WARNING", "warn to ignore", False),
|
||||
("WARNING", "warn to keep", True),
|
||||
("INFO", "info message", True),
|
||||
("ENLARGE", "enlarge to ignore", False),
|
||||
("ENLARGE", "other enlarge", True),
|
||||
])
|
||||
def test_ignore_log_msg_filter(capfd, level, msg, should_appear):
|
||||
# Clear captured output before logging
|
||||
capfd.readouterr()
|
||||
|
||||
# Set up filter
|
||||
with ignore_log_msg(log, lvl=level, msg=msg):
|
||||
# Trigger all log levels
|
||||
log.warning("warn to ignore")
|
||||
log.warning("warn to keep")
|
||||
log.info("info message")
|
||||
log.enlarge("enlarge to ignore")
|
||||
log.enlarge("other enlarge")
|
||||
|
||||
# Capture the output
|
||||
out = capfd.readouterr().out + capfd.readouterr().err
|
||||
|
||||
# Check if the message appears
|
||||
if should_appear:
|
||||
assert msg in out, f"Expected '{msg}' to appear in logs"
|
||||
else:
|
||||
assert msg not in out, f"Expected '{msg}' to be filtered from logs"
|
||||
|
||||
|
||||
def test_ignore_only_by_level(capfd):
|
||||
capfd.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg=None):
|
||||
log.warning("should be ignored")
|
||||
log.info("should appear")
|
||||
|
||||
out = capfd.readouterr().err
|
||||
assert "should be ignored" not in out
|
||||
assert "should appear" in out
|
||||
|
||||
|
||||
def test_ignore_only_by_msg(capfd):
|
||||
capfd.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl=None, msg="skip this"):
|
||||
log.warning("skip this")
|
||||
log.warning("keep this")
|
||||
|
||||
out = capfd.readouterr().err
|
||||
assert "skip this" not in out
|
||||
assert "keep this" in out
|
||||
|
||||
|
||||
def test_ignore_nothing_when_all_none(capfd):
|
||||
capfd.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl=None, msg=None):
|
||||
log.warning("warning")
|
||||
log.info("info")
|
||||
|
||||
out = capfd.readouterr().err
|
||||
assert "warning" not in out # because logcfg("DEBUG") sets level > warning by default
|
||||
assert "info" in out
|
||||
|
||||
|
||||
def test_filter_removed_after_context(capfd):
|
||||
capfd.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg="temp msg"):
|
||||
log.warning("temp msg")
|
||||
|
||||
# Now outside context: should appear again
|
||||
log.warning("temp msg")
|
||||
out = capfd.readouterr().err
|
||||
assert "temp msg" in out
|
||||
Reference in New Issue
Block a user