Add Log method "any handler is minimum level" with tests
Checks if a given handler is set for any current active handler
This commit is contained in:
@@ -438,4 +438,81 @@ class TestLogLevelHandling:
|
||||
level = log_instance.get_log_level("file_handler")
|
||||
assert level == LoggingLevel.DEBUG
|
||||
|
||||
|
||||
class DummyHandler:
|
||||
"""Dummy log level handler"""
|
||||
def __init__(self, level: LoggingLevel):
|
||||
self.level = level
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def log_instance_level() -> Log:
|
||||
"""
|
||||
Minimal log instance with dummy handlers
|
||||
|
||||
Returns:
|
||||
Log -- _description_
|
||||
"""
|
||||
log = Log(
|
||||
log_path=Path("/tmp/test.log"),
|
||||
log_name="test",
|
||||
log_settings={
|
||||
"log_level_console": LoggingLevel.DEBUG,
|
||||
"log_level_file": LoggingLevel.DEBUG,
|
||||
"console_enabled": False,
|
||||
"console_color_output_enabled": False,
|
||||
"console_format_type": None,
|
||||
"per_run_log": False,
|
||||
"add_start_info": False,
|
||||
"add_end_info": False,
|
||||
"log_queue": None,
|
||||
}
|
||||
)
|
||||
return log
|
||||
|
||||
|
||||
def test_any_handler_is_minimum_level_true(log_instance_level: Log):
|
||||
"""Test any_handler_is_minimum_level returns True when a handler meets the level"""
|
||||
# Handler with DEBUG level, should include INFO
|
||||
log_instance_level.handlers = {
|
||||
"h1": DummyHandler(LoggingLevel.DEBUG)
|
||||
}
|
||||
assert log_instance_level.any_handler_is_minimum_level(LoggingLevel.INFO) is True
|
||||
|
||||
|
||||
def test_any_handler_is_minimum_level_false(log_instance_level: Log):
|
||||
"""Test any_handler_is_minimum_level returns False when no handler meets the level"""
|
||||
# Handler with WARNING level, should include ERROR
|
||||
log_instance_level.handlers = {
|
||||
"h1": DummyHandler(LoggingLevel.WARNING)
|
||||
}
|
||||
assert log_instance_level.any_handler_is_minimum_level(LoggingLevel.ERROR) is True
|
||||
|
||||
|
||||
def test_any_handler_is_minimum_level_multiple(log_instance_level: Log):
|
||||
"""Test any_handler_is_minimum_level with multiple handlers"""
|
||||
# Multiple handlers, one matches
|
||||
log_instance_level.handlers = {
|
||||
"h1": DummyHandler(LoggingLevel.ERROR),
|
||||
"h2": DummyHandler(LoggingLevel.DEBUG)
|
||||
}
|
||||
assert log_instance_level.any_handler_is_minimum_level(LoggingLevel.INFO) is True
|
||||
# None matches
|
||||
log_instance_level.handlers = {
|
||||
"h1": DummyHandler(LoggingLevel.ERROR),
|
||||
"h2": DummyHandler(LoggingLevel.CRITICAL)
|
||||
}
|
||||
assert log_instance_level.any_handler_is_minimum_level(LoggingLevel.DEBUG) is False
|
||||
|
||||
|
||||
def test_any_handler_is_minimum_level_handles_exceptions(log_instance_level: Log):
|
||||
"""Test any_handler_is_minimum_level handles exceptions gracefully"""
|
||||
# Handler with missing level attribute
|
||||
class BadHandler:
|
||||
pass
|
||||
log_instance_level.handlers = {
|
||||
"h1": BadHandler()
|
||||
}
|
||||
# Should not raise, just return False
|
||||
assert log_instance_level.any_handler_is_minimum_level(LoggingLevel.DEBUG) is False
|
||||
# __END__
|
||||
|
||||
Reference in New Issue
Block a user