Spaces:
Running on Zero
Running on Zero
Download app/core/logging.py from Kaaado/agrovia-detection-api: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/Kaaado/agrovia-detection-api/resolve/main/app/core/logging.py
- Command line
-
hf download hf://spaces/Kaaado/agrovia-detection-api/app/core/logging.py
-
curl -L -o logging.py https://huggingface.co/spaces/Kaaado/agrovia-detection-api/resolve/main/app/core/logging.py
2.67 kB
| """ | |
| Detection Service — Structured Production Logging | |
| Uses structlog for JSON-formatted, correlation-ID-aware logs. | |
| Includes secret masking for production safety. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import re | |
| import sys | |
| from typing import Any | |
| import structlog | |
| # ── Secret masking patterns ── | |
| _SECRET_KEYS = re.compile( | |
| r"(password|secret|token|api_key|authorization|cookie)", | |
| re.IGNORECASE, | |
| ) | |
| _MASK = "***REDACTED***" | |
| def _mask_secrets( | |
| logger: Any, method_name: str, event_dict: dict | |
| ) -> dict: | |
| """Mask sensitive values in structured log events.""" | |
| for key in list(event_dict.keys()): | |
| if _SECRET_KEYS.search(key): | |
| val = event_dict[key] | |
| if isinstance(val, str) and len(val) > 8: | |
| event_dict[key] = val[:4] + _MASK | |
| else: | |
| event_dict[key] = _MASK | |
| return event_dict | |
| def setup_logging(*, level: str = "INFO", json_format: bool = True) -> None: | |
| """Configure structlog + stdlib logging for the application.""" | |
| shared_processors: list = [ | |
| structlog.contextvars.merge_contextvars, | |
| structlog.stdlib.add_log_level, | |
| structlog.stdlib.add_logger_name, | |
| structlog.processors.TimeStamper(fmt="iso"), | |
| structlog.processors.StackInfoRenderer(), | |
| structlog.processors.format_exc_info, | |
| structlog.processors.UnicodeDecoder(), | |
| _mask_secrets, | |
| ] | |
| if json_format: | |
| renderer = structlog.processors.JSONRenderer() | |
| else: | |
| renderer = structlog.dev.ConsoleRenderer(colors=True) | |
| structlog.configure( | |
| processors=[ | |
| *shared_processors, | |
| structlog.stdlib.ProcessorFormatter.wrap_for_formatter, | |
| ], | |
| logger_factory=structlog.stdlib.LoggerFactory(), | |
| wrapper_class=structlog.stdlib.BoundLogger, | |
| cache_logger_on_first_use=True, | |
| ) | |
| formatter = structlog.stdlib.ProcessorFormatter( | |
| processors=[ | |
| structlog.stdlib.ProcessorFormatter.remove_processors_meta, | |
| renderer, | |
| ], | |
| ) | |
| root = logging.getLogger() | |
| root.setLevel(getattr(logging, level.upper(), logging.INFO)) | |
| handler = logging.StreamHandler(sys.stdout) | |
| handler.setFormatter(formatter) | |
| # Clear existing handlers and set ours | |
| root.handlers.clear() | |
| root.addHandler(handler) | |
| # Quiet noisy libraries | |
| for lib in ("uvicorn.access", "onnxruntime", "tflite_runtime"): | |
| logging.getLogger(lib).setLevel(logging.WARNING) | |
| def get_logger(name: str | None = None) -> structlog.stdlib.BoundLogger: | |
| """Return a bound structlog logger.""" | |
| return structlog.get_logger(name) | |