SPB Git

spb/findata

Public
1.7 KB · 53 lines python
1from __future__ import annotations23import contextlib4import logging5import sys67import structlog89from findata_core.config import get_settings10from findata_core.redact import RedactingFilter111213def configure_logging(level: str = "INFO") -> None:14    settings = get_settings()15    logging.basicConfig(level=level, stream=sys.stdout, format="%(message)s")16    # Never let httpx/httpcore print request URLs (they contain the vendor user id).17    for noisy in ("httpx", "httpcore", "urllib3"):18        logging.getLogger(noisy).setLevel(logging.WARNING)19    structlog.configure(20        processors=[21            structlog.contextvars.merge_contextvars,22            structlog.processors.add_log_level,23            structlog.processors.TimeStamper(fmt="iso", utc=True),24            RedactingFilter(settings.secret_values()),25            structlog.processors.JSONRenderer() if settings.is_production else structlog.dev.ConsoleRenderer(),26        ],27        wrapper_class=structlog.make_filtering_bound_logger(logging.getLevelName(level)),28        logger_factory=_SafePrintLoggerFactory(),29        cache_logger_on_first_use=False,30    )313233class _SafePrintLogger:34    """stdout logger that survives a closed pipe/pty (EIO/EPIPE) so a hang-up never crashes a running job."""3536    def __init__(self):37        self._file = sys.stdout3839    def msg(self, message: str) -> None:40        with contextlib.suppress(OSError, ValueError):41            print(message, file=self._file, flush=True)4243    log = debug = info = warn = warning = error = critical = exception = fatal = msg444546class _SafePrintLoggerFactory:47    def __call__(self, *args):48        return _SafePrintLogger()495051def get_logger(name: str):  # type: ignore[no-untyped-def]52    return structlog.get_logger(name)53
Files · main
Menu

Theme