Files
The-Ouroboros/src/logging_config.py
Jiho Son d1750af80f
Some checks failed
CI / test (push) Has been cancelled
Add complete Ouroboros trading system with TDD test suite
Implement the full autonomous trading agent architecture:
- KIS broker with async API, token refresh, leaky bucket rate limiter, and hash key signing
- Gemini-powered decision engine with JSON parsing and confidence threshold enforcement
- Risk manager with circuit breaker (-3% P&L) and fat finger protection (30% cap)
- Evolution engine for self-improving strategy generation via failure analysis
- 35 passing tests written TDD-first covering risk, broker, and brain modules
- CI/CD pipeline, Docker multi-stage build, and AI agent context docs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 02:08:48 +09:00

43 lines
1.4 KiB
Python

"""JSON-formatted structured logging for machine readability."""
from __future__ import annotations
import logging
import sys
from datetime import datetime, timezone
from typing import Any
import json
class JSONFormatter(logging.Formatter):
"""Emit log records as single-line JSON objects."""
def format(self, record: logging.LogRecord) -> str:
log_entry: dict[str, Any] = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
}
if record.exc_info and record.exc_info[1]:
log_entry["exception"] = self.formatException(record.exc_info)
# Merge any extra fields attached to the record
for key in ("stock_code", "action", "confidence", "pnl_pct", "order_amount"):
value = getattr(record, key, None)
if value is not None:
log_entry[key] = value
return json.dumps(log_entry, ensure_ascii=False)
def setup_logging(level: int = logging.INFO) -> None:
"""Configure the root logger with JSON output to stdout."""
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
root = logging.getLogger()
root.setLevel(level)
# Avoid duplicate handlers on repeated calls
root.handlers.clear()
root.addHandler(handler)