Files
VianoBugAnalysis/src/viano_bug_analysis/llm/logging.py
T

32 lines
1.1 KiB
Python

import json
import logging
from datetime import datetime, timezone
from logging.handlers import RotatingFileHandler
from viano_bug_analysis.lifecycle.storage import key
from .errors import LLMError
class _JSONFormatter(logging.Formatter):
def format(self, record):
return json.dumps({"timestamp": datetime.fromtimestamp(record.created, timezone.utc).isoformat(), "level": record.levelname,
"message": record.getMessage()}, ensure_ascii=False)
def case_logger(workspace):
directory = workspace.runtime / "logs"
if directory.is_symlink():
raise LLMError("LLM_LOG_PATH")
directory.mkdir(exist_ok=True)
path = directory / "llm.jsonl"
if path.is_symlink():
raise LLMError("LLM_LOG_PATH")
logger = logging.getLogger("viano_bug_analysis.llm.cases." + key(str(path)))
logger.setLevel(logging.INFO)
if not logger.handlers:
handler = RotatingFileHandler(path, maxBytes=1024 * 1024, backupCount=3, encoding="utf-8")
handler.setFormatter(_JSONFormatter())
logger.addHandler(handler)
return logger