-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.py
More file actions
37 lines (28 loc) · 1.17 KB
/
Copy pathlogging.py
File metadata and controls
37 lines (28 loc) · 1.17 KB
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
from __future__ import annotations
import logging
import os
from typing import Literal
import structlog
LogLevel = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
def configure_structlog(*, level: LogLevel | None = None) -> None:
"""Configure structlog for JSON logs.
This keeps configuration centralized so CLI tools and FastAPI share the same
log shape (helpful for production observability and audit trails).
Args:
level: Optional log level override. Defaults to `LOG_LEVEL` env var or INFO.
"""
resolved_level = (level or os.getenv("LOG_LEVEL", "INFO")).upper()
logging.basicConfig(level=getattr(logging, resolved_level, logging.INFO))
structlog.configure(
processors=[
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.JSONRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(
getattr(logging, resolved_level, logging.INFO)
),
cache_logger_on_first_use=True,
)