Spaces:
Paused
Paused
Download src/core/logging.py from arimatch1/crewhub-api-2: direct link, hf CLI and curl.
- Browser
- Download file 1.99 kB
-
https://huggingface.co/spaces/arimatch1/crewhub-api-2/resolve/main/src/core/logging.py
- Command line
-
hf download hf://spaces/arimatch1/crewhub-api-2/src/core/logging.py
-
curl -L -o logging.py https://huggingface.co/spaces/arimatch1/crewhub-api-2/resolve/main/src/core/logging.py
1.99 kB
| # Copyright (c) 2026 CrewHub. All rights reserved. | |
| # Proprietary and confidential. See LICENSE for details. | |
| """Structured logging configuration. | |
| Supports two formats: | |
| - "json": Machine-parseable JSON lines (for Cloud Run / observability pipelines) | |
| - "text": Human-readable format (for local development) | |
| """ | |
| import json | |
| import logging | |
| import sys | |
| from datetime import datetime, timezone | |
| class JSONFormatter(logging.Formatter): | |
| """Emit each log record as a single JSON line.""" | |
| def format(self, record: logging.LogRecord) -> str: | |
| log_entry = { | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| "level": record.levelname, | |
| "logger": record.name, | |
| "message": record.getMessage(), | |
| } | |
| # Include request_id from middleware if present | |
| if hasattr(record, "request_id"): | |
| log_entry["request_id"] = record.request_id | |
| if hasattr(record, "duration_ms"): | |
| log_entry["duration_ms"] = record.duration_ms | |
| if record.exc_info and record.exc_info[0] is not None: | |
| log_entry["exception"] = self.formatException(record.exc_info) | |
| return json.dumps(log_entry) | |
| def setup_logging(level: str = "INFO", fmt: str = "json") -> None: | |
| """Configure the root logger with the specified level and format. | |
| Args: | |
| level: Logging level name (DEBUG, INFO, WARNING, ERROR, CRITICAL). | |
| fmt: Output format — "json" for structured JSON lines, "text" for | |
| human-readable output. | |
| """ | |
| root = logging.getLogger() | |
| root.setLevel(getattr(logging, level.upper(), logging.INFO)) | |
| # Remove existing handlers to avoid duplicate output | |
| root.handlers.clear() | |
| handler = logging.StreamHandler(sys.stdout) | |
| if fmt == "json": | |
| handler.setFormatter(JSONFormatter()) | |
| else: | |
| handler.setFormatter( | |
| logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s") | |
| ) | |
| root.addHandler(handler) | |