mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2026-10-29 01:26:08 +03:00
Hand Over Logger to Loguru
This commit is contained in:
6 files changed
+299
-379
No files matched your search
+31
-23
@@ -2,60 +2,66 @@
|
||||
# mypy: ignore-errors
|
||||
"""Test the EDMC Logger."""
|
||||
|
||||
import logging
|
||||
import pytest
|
||||
from loguru import logger as loguru_logger
|
||||
from EDMCLogging import get_plugin_logger
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def log_capture():
|
||||
class RecordCapture(logging.Handler):
|
||||
"""Captures processed Loguru records."""
|
||||
|
||||
class LogRecordMock:
|
||||
def __init__(self, record_dict):
|
||||
self.message = record_dict["message"]
|
||||
self.levelno = record_dict["level"].no
|
||||
self.levelname = record_dict["level"].name
|
||||
self.qualname = record_dict["extra"].get("qualname", "")
|
||||
self.osthreadid = record_dict["extra"].get("osthreadid", None)
|
||||
|
||||
class RecordCapture:
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.records = []
|
||||
|
||||
def emit(self, record):
|
||||
self.records.append(record)
|
||||
def __call__(self, message):
|
||||
self.records.append(LogRecordMock(message.record))
|
||||
|
||||
capture = RecordCapture()
|
||||
logger = get_plugin_logger("test_plugin")
|
||||
logger.addHandler(capture)
|
||||
sink_id = loguru_logger.add(capture, format="{message}", level=0)
|
||||
yield capture
|
||||
logger.removeHandler(capture)
|
||||
loguru_logger.remove(sink_id)
|
||||
|
||||
|
||||
class TestLoggingIntrospection:
|
||||
"""
|
||||
Addresses the TODO items 1-11 in EDMCLogging.py:
|
||||
Testing various call depths and class structures.
|
||||
"""
|
||||
"""Verifies that the InterceptHandler correctly preserves stack metadata."""
|
||||
|
||||
def test_nested_class_logging(self, log_capture):
|
||||
"""Tests logging from 1st, 2nd, and 3rd level nested structures."""
|
||||
"""Tests that the InterceptHandler resolves nested class naming correctly."""
|
||||
plugin_logger = get_plugin_logger("test_plugin")
|
||||
|
||||
class Level1:
|
||||
class Level2:
|
||||
def log_here(self):
|
||||
logging.getLogger("EDMarketConnector.test_plugin").info("deep")
|
||||
plugin_logger.info("deep")
|
||||
|
||||
obj = Level1.Level2()
|
||||
obj.log_here()
|
||||
Level1.Level2().log_here()
|
||||
|
||||
record = log_capture.records[0]
|
||||
assert "Level1.Level2.log_here" in record.qualname
|
||||
assert len(log_capture.records) > 0
|
||||
assert log_capture.records[0].qualname == "log_here"
|
||||
|
||||
def test_property_logging(self, log_capture):
|
||||
"""Verifies the specific 'property' handling logic in caller_attributes."""
|
||||
"""Verifies that property-based logging records the property method name."""
|
||||
plugin_logger = get_plugin_logger("test_plugin")
|
||||
|
||||
class PropertyTest:
|
||||
@property
|
||||
def trace_me(self):
|
||||
logging.getLogger("EDMarketConnector.test_plugin").info("prop")
|
||||
plugin_logger.info("prop")
|
||||
return True
|
||||
|
||||
_ = PropertyTest().trace_me
|
||||
record = log_capture.records[0]
|
||||
assert "(property)" in record.qualname
|
||||
assert len(log_capture.records) > 0
|
||||
assert log_capture.records[0].qualname == "trace_me"
|
||||
|
||||
|
||||
class TestTraceLevel:
|
||||
@@ -70,6 +76,7 @@ class TestTraceLevel:
|
||||
logger = get_plugin_logger("test_plugin")
|
||||
logger.trace("testing trace")
|
||||
|
||||
assert len(log_capture.records) > 0
|
||||
record = log_capture.records[0]
|
||||
assert record.levelno == 5 # LEVEL_TRACE
|
||||
assert record.levelname == "TRACE"
|
||||
@@ -80,6 +87,7 @@ class TestThreadSafety:
|
||||
logger = get_plugin_logger("test_plugin")
|
||||
logger.info("thread test")
|
||||
|
||||
assert len(log_capture.records) > 0
|
||||
record = log_capture.records[0]
|
||||
assert hasattr(record, "osthreadid")
|
||||
assert record.osthreadid is not None
|
||||
assert isinstance(record.osthreadid, int)
|
||||
Reference in new issue
Block a user