Buckets:

download
raw
3.04 kB
# Copyright (c) 2025-2026 Yutaro Maeda. All rights reserved.
# Licensed under the Apache License 2.0. See LICENSE file for details.
"""Structured logging configuration for FaultRay.
Provides JSON-formatted structured logging for production use,
with human-readable fallback for development.
"""
from __future__ import annotations
import json
import logging
import sys
from datetime import datetime, timezone
from typing import Optional
class JSONFormatter(logging.Formatter):
"""Format log records as JSON for structured logging pipelines."""
def format(self, record: logging.LogRecord) -> str:
log_entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
"function": record.funcName,
"line": record.lineno,
}
if record.exc_info and record.exc_info[0]:
log_entry["exception"] = self.formatException(record.exc_info)
# Include extra fields
for key in ("component", "scenario", "engine", "duration_ms", "score"):
if hasattr(record, key):
log_entry[key] = getattr(record, key)
return json.dumps(log_entry, default=str)
class HumanFormatter(logging.Formatter):
"""Human-readable formatter for development."""
COLORS = {
"DEBUG": "\033[36m", # Cyan
"INFO": "\033[32m", # Green
"WARNING": "\033[33m", # Yellow
"ERROR": "\033[31m", # Red
"CRITICAL": "\033[35m", # Magenta
}
RESET = "\033[0m"
def format(self, record: logging.LogRecord) -> str:
color = self.COLORS.get(record.levelname, "")
timestamp = datetime.now().strftime("%H:%M:%S")
return f"{color}{timestamp} [{record.levelname:8s}]{self.RESET} {record.getMessage()}"
def setup_logging(
level: str = "INFO",
json_output: bool = False,
log_file: Optional[str] = None,
) -> logging.Logger:
"""Configure FaultRay logging.
Args:
level: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
json_output: Use JSON format (for production/pipelines)
log_file: Optional file path for log output
Returns:
Configured root logger for faultray
"""
logger = logging.getLogger("faultray")
logger.setLevel(getattr(logging, level.upper(), logging.INFO))
logger.handlers.clear()
formatter = JSONFormatter() if json_output else HumanFormatter()
# Console handler
console = logging.StreamHandler(sys.stderr)
console.setFormatter(formatter)
logger.addHandler(console)
# File handler (always JSON for machine parsing)
if log_file:
fh = logging.FileHandler(log_file)
fh.setFormatter(JSONFormatter())
logger.addHandler(fh)
return logger
def get_logger(name: str) -> logging.Logger:
"""Get a child logger for a FaultRay module."""
return logging.getLogger(f"faultray.{name}")

Xet Storage Details

Size:
3.04 kB
·
Xet hash:
81a43b0d01614a79236d5fcc59cf07b4d908c542904f662e667e9c24482a6012

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.