코드리뷰 수정
This commit is contained in:
@@ -1,14 +1,36 @@
|
||||
"""공통 로깅 설정 — KST(Asia/Seoul) 타임존 포매터 제공."""
|
||||
"""공통 로깅 설정 — KST(Asia/Seoul) 타임존 포매터 + 민감 정보 마스킹 제공."""
|
||||
|
||||
import logging
|
||||
import datetime
|
||||
import re
|
||||
import pytz
|
||||
|
||||
KST = pytz.timezone("Asia/Seoul")
|
||||
|
||||
# ── 민감 정보 마스킹 패턴 (API 키, 시크릿, 토큰, 계좌번호) ──────────────────────
|
||||
_MASK_PATTERNS = [
|
||||
# Bearer 토큰
|
||||
(re.compile(r'(Bearer\s+)[A-Za-z0-9\-_.]{20,}', re.I), r'\1***'),
|
||||
# access_token 값
|
||||
(re.compile(r'(access_?token[":\s=]+)[A-Za-z0-9\-_.]{20,}', re.I), r'\1***'),
|
||||
# appsecret / app_secret 값
|
||||
(re.compile(r'(app_?secret[":\s=]+)[A-Za-z0-9+/=]{10,}', re.I), r'\1***'),
|
||||
# appkey / app_key 값 (8자 이상)
|
||||
(re.compile(r'(app_?key[":\s=]+)[A-Za-z0-9]{8,}', re.I), r'\1***'),
|
||||
# 계좌번호 (account_no / account_number)
|
||||
(re.compile(r'(account[_\s]?(?:no|number)[":\s=]+)\d{6,}', re.I), r'\1***'),
|
||||
]
|
||||
|
||||
|
||||
def _mask_sensitive(text: str) -> str:
|
||||
"""로그 텍스트에서 API 키·토큰·계좌번호 등 민감 정보를 마스킹한다."""
|
||||
for pattern, replacement in _MASK_PATTERNS:
|
||||
text = pattern.sub(replacement, text)
|
||||
return text
|
||||
|
||||
|
||||
class KSTFormatter(logging.Formatter):
|
||||
"""%(asctime)s 를 KST(UTC+9) 기준으로 출력하는 포매터."""
|
||||
"""%(asctime)s 를 KST(UTC+9) 기준으로 출력하고, 민감 정보를 마스킹하는 포매터."""
|
||||
|
||||
def formatTime(self, record: logging.LogRecord, datefmt: str | None = None) -> str:
|
||||
ct = datetime.datetime.fromtimestamp(record.created, tz=KST)
|
||||
@@ -16,3 +38,8 @@ class KSTFormatter(logging.Formatter):
|
||||
return ct.strftime(datefmt)
|
||||
t = ct.strftime(self.default_time_format)
|
||||
return self.default_msec_format % (t, record.msecs)
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
"""포맷팅 후 민감 정보 마스킹 적용."""
|
||||
formatted = super().format(record)
|
||||
return _mask_sensitive(formatted)
|
||||
|
||||
Reference in New Issue
Block a user