This commit is contained in:
+37
-19
@@ -8,40 +8,53 @@ from slic.utils.logign import *
|
||||
from slic.utils.logcfg import add_log_Level, logcfg
|
||||
|
||||
import logzero
|
||||
from logzero import logger as log, LogFormatter
|
||||
from logzero import logger as log
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_logging():
|
||||
# Clear existing handlers for pytest capture
|
||||
for handler in log.handlers[:]:
|
||||
log.removeHandler(handler)
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
log.addHandler(handler)
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def setup_custom_levels():
|
||||
add_log_Level(log, "ENLARGE", logging.INFO + 5, func_name="enlarge")
|
||||
logcfg("DEBUG") # To ensure all logs appear
|
||||
log.setLevel(logging.DEBUG)
|
||||
|
||||
# Parametrized test for various log levels and messages
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_log_handlers():
|
||||
# Reset logger handlers before each test
|
||||
for h in log.handlers[:]:
|
||||
log.removeHandler(h)
|
||||
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
formatter = logging.Formatter('%(levelname)s:%(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
log.addHandler(handler)
|
||||
|
||||
# --- Tests ---
|
||||
|
||||
@pytest.mark.parametrize("level,msg,should_appear", [
|
||||
("WARNING", "warn to ignore", False),
|
||||
("WARNING", "warn to keep", True),
|
||||
("INFO", "info message", True),
|
||||
("WARNING", "another warning", True),
|
||||
("ENLARGE", "enlarge to ignore", False),
|
||||
("ENLARGE", "other enlarge", True),
|
||||
])
|
||||
def test_ignore_log_msg_exact_match(capsys, level, msg, should_appear):
|
||||
capsys.readouterr() # Clear previous capture
|
||||
def test_ignore_log_msg_exact(level, msg, should_appear, capsys):
|
||||
capsys.readouterr() # Clear previous logs
|
||||
|
||||
with ignore_log_msg(log, lvl=level, msg=msg):
|
||||
log.warning("warn to ignore")
|
||||
log.warning("warn to keep")
|
||||
log.info("info message")
|
||||
log.warning("another warning")
|
||||
log.enlarge("enlarge to ignore")
|
||||
log.enlarge("other enlarge")
|
||||
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
|
||||
if should_appear:
|
||||
assert msg in captured, f"'{msg}' should appear in logs"
|
||||
else:
|
||||
assert msg not in captured, f"'{msg}' should be filtered out"
|
||||
assert msg not in captured, f"'{msg}' should NOT appear in logs"
|
||||
|
||||
# Test filtering only by log level
|
||||
def test_ignore_only_by_level(capsys):
|
||||
capsys.readouterr()
|
||||
|
||||
@@ -50,10 +63,11 @@ def test_ignore_only_by_level(capsys):
|
||||
log.info("should appear")
|
||||
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
|
||||
assert "should be ignored" not in captured
|
||||
assert "should appear" in captured
|
||||
|
||||
# Test filtering only by message content
|
||||
def test_ignore_only_by_msg(capsys):
|
||||
capsys.readouterr()
|
||||
|
||||
@@ -62,18 +76,22 @@ def test_ignore_only_by_msg(capsys):
|
||||
log.warning("keep this")
|
||||
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
|
||||
assert "skip this" not in captured
|
||||
assert "keep this" in captured
|
||||
|
||||
# Test that filter is removed after exiting the context manager
|
||||
def test_filter_removed_after_context(capsys):
|
||||
capsys.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg="temp msg"):
|
||||
log.warning("temp msg")
|
||||
log.warning("temp msg") # This should be filtered
|
||||
|
||||
# Outside the context: the message should now be logged
|
||||
# Outside context — should appear
|
||||
log.warning("temp msg")
|
||||
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
|
||||
assert "temp msg" in captured
|
||||
|
||||
|
||||
Reference in New Issue
Block a user