Initial public release
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import logging
|
||||
import structlog
|
||||
|
||||
|
||||
def _mask_secrets(_, __, event_dict: dict) -> dict:
|
||||
"""API 키 등 민감 정보를 로그에서 마스킹"""
|
||||
sensitive = ("api_key", "secret", "token", "password", "key")
|
||||
for k in list(event_dict.keys()):
|
||||
if any(s in k.lower() for s in sensitive):
|
||||
event_dict[k] = "***MASKED***"
|
||||
return event_dict
|
||||
|
||||
|
||||
def configure_logging(level: str = "INFO") -> None:
|
||||
"""structlog JSON 로깅 설정. 애플리케이션 시작 시 1회 호출."""
|
||||
structlog.configure(
|
||||
processors=[
|
||||
structlog.contextvars.merge_contextvars,
|
||||
structlog.stdlib.add_log_level,
|
||||
structlog.stdlib.add_logger_name,
|
||||
structlog.processors.TimeStamper(fmt="iso"),
|
||||
_mask_secrets,
|
||||
structlog.processors.JSONRenderer(),
|
||||
],
|
||||
wrapper_class=structlog.make_filtering_bound_logger(
|
||||
getattr(logging, level.upper(), logging.INFO)
|
||||
),
|
||||
context_class=dict,
|
||||
logger_factory=structlog.PrintLoggerFactory(),
|
||||
)
|
||||
|
||||
|
||||
def get_logger(name: str):
|
||||
return structlog.get_logger(name)
|
||||
Reference in New Issue
Block a user