53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
"""Logging setup for IoT Bridge."""
|
|
|
|
import structlog
|
|
|
|
from config import LoggingConfig
|
|
|
|
|
|
LOG_LEVELS: dict[str, int] = {
|
|
"critical": 50,
|
|
"error": 40,
|
|
"warning": 30,
|
|
"info": 20,
|
|
"debug": 10,
|
|
"notset": 0,
|
|
}
|
|
|
|
|
|
def setup_logging(config: LoggingConfig):
|
|
"""Configure structured logging with structlog.
|
|
|
|
Args:
|
|
config: Logging configuration including level and output format.
|
|
|
|
Returns:
|
|
Configured structlog logger instance.
|
|
"""
|
|
|
|
# Set log level
|
|
log_level = LOG_LEVELS.get(config.level.lower(), LOG_LEVELS["info"])
|
|
|
|
renderer_processor = (
|
|
structlog.processors.JSONRenderer()
|
|
if config.format == "json"
|
|
else structlog.dev.ConsoleRenderer()
|
|
)
|
|
|
|
structlog.configure(
|
|
processors=[
|
|
structlog.contextvars.merge_contextvars,
|
|
structlog.processors.add_log_level,
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.processors.StackInfoRenderer(),
|
|
structlog.processors.format_exc_info,
|
|
renderer_processor,
|
|
],
|
|
context_class=dict,
|
|
wrapper_class=structlog.make_filtering_bound_logger(log_level),
|
|
logger_factory=structlog.PrintLoggerFactory(),
|
|
cache_logger_on_first_use=True,
|
|
)
|
|
|
|
return structlog.get_logger()
|