This commit is contained in:
+25
-63
@@ -9,89 +9,51 @@ from slic.utils.logcfg import add_log_Level, logcfg
|
||||
|
||||
import logzero
|
||||
|
||||
log = logging.getLogger("test_logger")
|
||||
import logging
|
||||
import sys
|
||||
import pytest
|
||||
from logzero import logger as log
|
||||
from slic.utils.logign import ignore_log_msg
|
||||
from slic.utils.logcfg import add_log_Level, logcfg
|
||||
|
||||
@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)
|
||||
logcfg("DEBUG") # Allow everything
|
||||
|
||||
@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')
|
||||
formatter = logzero.LogFormatter()
|
||||
handler.setFormatter(formatter)
|
||||
log.addHandler(handler)
|
||||
log.setLevel(logging.DEBUG)
|
||||
|
||||
# --- Tests ---
|
||||
|
||||
@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_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.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 NOT appear in logs"
|
||||
|
||||
def test_ignore_only_by_level(capsys):
|
||||
def test_ignore_log_msg_works(capsys):
|
||||
capsys.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg=None):
|
||||
log.warning("should be ignored")
|
||||
log.info("should appear")
|
||||
|
||||
# Log without filter
|
||||
log.warning("visible log")
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
print("Captured 1:", repr(captured))
|
||||
assert "visible log" in captured
|
||||
|
||||
assert "should be ignored" not in captured
|
||||
assert "should appear" in captured
|
||||
|
||||
def test_ignore_only_by_msg(capsys):
|
||||
# Log inside context (should be hidden)
|
||||
capsys.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl=None, msg="skip this"):
|
||||
log.warning("skip this")
|
||||
log.warning("keep this")
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg="hide me"):
|
||||
log.warning("hide me")
|
||||
log.warning("keep me")
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
print("Captured 2:", repr(captured))
|
||||
assert "hide me" not in captured
|
||||
assert "keep me" in captured
|
||||
|
||||
assert "skip this" not in captured
|
||||
assert "keep this" in captured
|
||||
|
||||
def test_filter_removed_after_context(capsys):
|
||||
# Log again after context (should be shown)
|
||||
capsys.readouterr()
|
||||
|
||||
with ignore_log_msg(log, lvl="WARNING", msg="temp msg"):
|
||||
log.warning("temp msg") # This should be filtered
|
||||
|
||||
# Outside context — should appear
|
||||
log.warning("temp msg")
|
||||
|
||||
log.warning("hide me")
|
||||
captured = capsys.readouterr().err
|
||||
print("Captured stderr:", repr(captured))
|
||||
|
||||
assert "temp msg" in captured
|
||||
|
||||
print("Captured 3:", repr(captured))
|
||||
assert "hide me" in captured
|
||||
|
||||
Reference in New Issue
Block a user