Compare commits
2 Commits
74c10b719b
...
140b0d5404
| Author | SHA1 | Date | |
|---|---|---|---|
| 140b0d5404 | |||
| fc464afaf5 |
@@ -0,0 +1,40 @@
|
|||||||
|
"""Logging-Konfiguration mit structlog für strukturierte Logs."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
|
import structlog
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging() -> None:
|
||||||
|
"""Konfiguriert strukturiertes Logging mit structlog."""
|
||||||
|
structlog.configure(
|
||||||
|
processors=[
|
||||||
|
structlog.stdlib.filter_by_level,
|
||||||
|
structlog.stdlib.add_logger_name,
|
||||||
|
structlog.stdlib.add_log_level,
|
||||||
|
structlog.stdlib.PositionalArgumentsFormatter(),
|
||||||
|
structlog.processors.TimeStamper(fmt="iso"),
|
||||||
|
structlog.processors.StackInfoRenderer(),
|
||||||
|
structlog.processors.format_exc_info,
|
||||||
|
structlog.processors.UnicodeDecoder(),
|
||||||
|
structlog.dev.ConsoleRenderer(),
|
||||||
|
],
|
||||||
|
context_class=dict,
|
||||||
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
||||||
|
wrapper_class=structlog.stdlib.BoundLogger,
|
||||||
|
cache_logger_on_first_use=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_logger(name: str) -> structlog.stdlib.BoundLogger:
|
||||||
|
"""Erstellt einen benannten Logger.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: Logger-Name (üblicherweise __name__).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Konfigurierter structlog-Logger.
|
||||||
|
"""
|
||||||
|
return cast(structlog.stdlib.BoundLogger, structlog.get_logger(name))
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
"""Tests für Logging-Konfiguration."""
|
||||||
|
|
||||||
|
import structlog
|
||||||
|
|
||||||
|
from src.logging_config import get_logger, setup_logging
|
||||||
|
|
||||||
|
|
||||||
|
class TestLoggingConfig:
|
||||||
|
"""Tests für logging_config."""
|
||||||
|
|
||||||
|
def test_setup_logging_laeuft_ohne_fehler_durch(self) -> None:
|
||||||
|
"""setup_logging() läuft ohne Fehler."""
|
||||||
|
setup_logging() # Sollte keine Exception werfen
|
||||||
|
|
||||||
|
def test_get_logger_gibt_bound_logger_zurueck(self) -> None:
|
||||||
|
"""get_logger(__name__) gibt Logger-ähnliches Objekt zurück."""
|
||||||
|
logger = get_logger("test_logger")
|
||||||
|
# Structlog gibt BoundLoggerLazyProxy zurück (lazy evaluation)
|
||||||
|
assert hasattr(logger, "info")
|
||||||
|
assert hasattr(logger, "warning")
|
||||||
|
|
||||||
|
def test_logger_info_produziert_keinen_fehler(self) -> None:
|
||||||
|
"""Logger.info() produziert keinen Fehler."""
|
||||||
|
logger = get_logger("test_logger")
|
||||||
|
logger.info("test message") # Sollte keine Exception werfen
|
||||||
Reference in New Issue
Block a user