Source code for autogen_agentchat.logging._file_log_handler

import json
import logging
from datetime import datetime

from pydantic import BaseModel


[docs] class FileLogHandler(logging.Handler): def __init__(self, filename: str) -> None: super().__init__() self.filename = filename self.file_handler = logging.FileHandler(filename)
[docs] def emit(self, record: logging.LogRecord) -> None: ts = datetime.fromtimestamp(record.created).isoformat() if isinstance(record.msg, BaseModel): record.msg = json.dumps( { "timestamp": ts, "message": record.msg.model_dump(), "type": record.msg.__class__.__name__, }, ) self.file_handler.emit(record)