Spaces:
Sleeping
Sleeping
File size: 1,825 Bytes
2d9b352 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | """
Logger Configuration Module
This module configures structured logging for the API Comparator service using structlog.
It provides customized log formatting with source file information and exception handling.
"""
import logging
import re
from typing import Dict, Any
import structlog
def custom_processor(_, __, event_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Process log events to add source file information and handle exceptions.
Args:
_: Unused logger parameter
__: Unused name parameter
event_dict: The log event dictionary to process
Returns:
Dict[str, Any]: The processed log event dictionary with added source information
"""
event_dict["source"] = f"{event_dict.pop('filename')}:{event_dict.pop('lineno')}"
if event_dict.get("level") == "error" and "exception" in event_dict:
exception_info = event_dict.pop("exception")
match = re.search(r'File \".*?\", line (\d+)', exception_info)
if match:
event_dict["source"] = f"{event_dict['source'].split(':')[0]}:{match.group(1)}"
return event_dict
# Configure structlog
structlog.configure(
processors=[
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.CallsiteParameterAdder(
[structlog.processors.CallsiteParameter.FILENAME,
structlog.processors.CallsiteParameter.LINENO]
),
custom_processor,
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=True,
)
|