-
Sentry LoggingPython 2026. 1. 3. 03:33
원래 Sentry는 에러 이벤트 추적기용으로 나왔다.
하지만 최근 Sentry에서 로그 뷰어 역할까지 할 수 있도록 업데이트되면서
ELK와 비슷하게 로그를 추적할 수 있는 사용자 친화적인 UI를 제공해주고 있다.
[Sentry에 로그 찍는 방법]
1. SDK에서 제공하는 Sentry 전용 Logger 사용
from sentry_sdk import logger as sentry_logger sentry_logger.trace('Starting database connection {database}', database="users") sentry_logger.debug('Cache miss for user {user_id}', user_id=123) sentry_logger.info('Updated global cache') sentry_logger.warning('Rate limit reached for endpoint {endpoint}', endpoint='/api/results/') sentry_logger.error('Failed to process payment. Order: {order_id}. Amount: {amount}', order_id="or_2342", amount=99.99) sentry_logger.fatal('Database {database} connection pool exhausted', database="users")SDK에서 직접 제공하는 로거를 사용하면 Sentry DSN을 참고하여 Sentry로 직접 보낸다.
2. Python의 Logging Module을 가로채어 Sentry에도 보내주는 방법
def init_sentry(): sentry_dsn = os.getenv("SENTRY_DSN") if not sentry_dsn: logging.warning("SENTRY_DSN이 설정되지 않았습니다. Sentry 로깅이 비활성화됩니다.") return # Python Logging System에 Sentry 적용 # BreadCrumb는 Sentry에 찍히지 않음 # Event가 발생하면 해당 이벤트의 breadcrumb으로 INFO/WARNING 로그들이 타임라인으로 같이 붙음 sentry_logging = LoggingIntegration( level = logging.INFO, event_level = logging.ERROR ) sentry_sdk.init( dsn=sentry_dsn, traces_sample_rate=1.0, enable_logs=True, # Only send INFO (and higher) logs to Sentry logs, # even if the logger is set to a lower level. integrations=[sentry_logging] )LoggingIntegration을 사용하면 Sentry가 기존 Python의 Logging Module에 끼어들어
Sentry에도 로그 메시지를 보낼 수 있도록 한다.
즉, 해당 설정을 추가하면 로그가 두 곳에 추가되는 것이다.
1) 기본 Python Module을 사용한 로깅
2) Sentry를 사용한 로깅
'Python' 카테고리의 다른 글
TypedDict vs Pydantic BaseModel (0) 2026.01.29 SQLAlchemy (0) 2026.01.23 ContextVar (0) 2026.01.03 async with / async for (0) 2025.12.13 StreamingResponse (0) 2025.11.15