Skip to content

FileLogger

autogen.logger.FileLogger #

FileLogger(config)

Bases: BaseLogger

Source code in autogen/logger/file_logger.py
def __init__(self, config: dict[str, Any]):
    self.config = config
    self.session_id = str(uuid.uuid4())

    curr_dir = os.getcwd()
    self.log_dir = os.path.join(curr_dir, "autogen_logs")
    os.makedirs(self.log_dir, exist_ok=True)

    self.log_file = os.path.join(self.log_dir, self.config.get("filename", "runtime.log"))
    try:
        with open(self.log_file, "a"):
            pass
    except Exception as e:
        logger.error(f"[file_logger] Failed to create logging file: {e}")

    self.logger = logging.getLogger(__name__)
    self.logger.setLevel(logging.INFO)
    file_handler = logging.FileHandler(self.log_file)
    self.logger.addHandler(file_handler)

config instance-attribute #

config = config

session_id instance-attribute #

session_id = str(uuid4())

log_dir instance-attribute #

log_dir = join(curr_dir, 'autogen_logs')

log_file instance-attribute #

log_file = join(log_dir, get('filename', 'runtime.log'))

logger instance-attribute #

logger = getLogger(__name__)

start #

start()

Start the logger and return the session_id.

Source code in autogen/logger/file_logger.py
def start(self) -> str:
    """Start the logger and return the session_id."""
    try:
        self.logger.info(f"Started new session with Session ID: {self.session_id}")
    except Exception as e:
        logger.error(f"[file_logger] Failed to create logging file: {e}")
    finally:
        return self.session_id

log_chat_completion #

log_chat_completion(invocation_id, client_id, wrapper_id, source, request, response, is_cached, cost, start_time)

Log a chat completion.

Source code in autogen/logger/file_logger.py
def log_chat_completion(
    self,
    invocation_id: uuid.UUID,
    client_id: int,
    wrapper_id: int,
    source: str | Agent,
    request: dict[str, float | str | list[dict[str, str]]],
    response: str | ChatCompletion,
    is_cached: int,
    cost: float,
    start_time: str,
) -> None:
    """Log a chat completion."""
    thread_id = threading.get_ident()
    source_name = (
        source
        if isinstance(source, str)
        else source.name
        if hasattr(source, "name") and source.name is not None
        else ""
    )
    try:
        log_data = json.dumps({
            "invocation_id": str(invocation_id),
            "client_id": client_id,
            "wrapper_id": wrapper_id,
            "request": to_dict(request),
            "response": str(response),
            "is_cached": is_cached,
            "cost": cost,
            "start_time": start_time,
            "end_time": get_current_ts(),
            "thread_id": thread_id,
            "source_name": source_name,
        })

        self.logger.info(log_data)
    except Exception as e:
        self.logger.error(f"[file_logger] Failed to log chat completion: {e}")

log_new_agent #

log_new_agent(agent, init_args={})

Log a new agent instance.

Source code in autogen/logger/file_logger.py
def log_new_agent(self, agent: ConversableAgent, init_args: dict[str, Any] = {}) -> None:
    """Log a new agent instance."""
    thread_id = threading.get_ident()

    try:
        log_data = json.dumps({
            "id": id(agent),
            "agent_name": agent.name if hasattr(agent, "name") and agent.name is not None else "",
            "wrapper_id": to_dict(
                agent.client.wrapper_id if hasattr(agent, "client") and agent.client is not None else ""
            ),
            "session_id": self.session_id,
            "current_time": get_current_ts(),
            "agent_type": type(agent).__name__,
            "args": to_dict(init_args),
            "thread_id": thread_id,
        })
        self.logger.info(log_data)
    except Exception as e:
        self.logger.error(f"[file_logger] Failed to log new agent: {e}")

log_event #

log_event(source, name, **kwargs)

Log an event from an agent or a string source.

Source code in autogen/logger/file_logger.py
def log_event(self, source: str | Agent, name: str, **kwargs: dict[str, Any]) -> None:
    """Log an event from an agent or a string source."""
    from .. import Agent

    # This takes an object o as input and returns a string. If the object o cannot be serialized, instead of raising an error,
    # it returns a string indicating that the object is non-serializable, along with its type's qualified name obtained using __qualname__.
    json_args = json.dumps(kwargs, default=lambda o: f"<<non-serializable: {type(o).__qualname__}>>")
    thread_id = threading.get_ident()

    if isinstance(source, Agent):
        try:
            log_data = json.dumps({
                "source_id": id(source),
                "source_name": str(source.name) if hasattr(source, "name") else source,
                "event_name": name,
                "agent_module": source.__module__,
                "agent_class": source.__class__.__name__,
                "json_state": json_args,
                "timestamp": get_current_ts(),
                "thread_id": thread_id,
            })
            self.logger.info(log_data)
        except Exception as e:
            self.logger.error(f"[file_logger] Failed to log event {e}")
    else:
        try:
            log_data = json.dumps({
                "source_id": id(source),
                "source_name": str(source.name) if hasattr(source, "name") else source,
                "event_name": name,
                "json_state": json_args,
                "timestamp": get_current_ts(),
                "thread_id": thread_id,
            })
            self.logger.info(log_data)
        except Exception as e:
            self.logger.error(f"[file_logger] Failed to log event {e}")

log_new_wrapper #

log_new_wrapper(wrapper, init_args={})

Log a new wrapper instance.

Source code in autogen/logger/file_logger.py
def log_new_wrapper(self, wrapper: OpenAIWrapper, init_args: dict[str, LLMConfig | list[LLMConfig]] = {}) -> None:
    """Log a new wrapper instance."""
    thread_id = threading.get_ident()

    try:
        log_data = json.dumps({
            "wrapper_id": id(wrapper),
            "session_id": self.session_id,
            "json_state": json.dumps(init_args),
            "timestamp": get_current_ts(),
            "thread_id": thread_id,
        })
        self.logger.info(log_data)
    except Exception as e:
        self.logger.error(f"[file_logger] Failed to log event {e}")

log_new_client #

log_new_client(client, wrapper, init_args)

Log a new client instance.

Source code in autogen/logger/file_logger.py
def log_new_client(
    self,
    client: (
        AzureOpenAI
        | OpenAI
        | CerebrasClient
        | GeminiClient
        | AnthropicClient
        | MistralAIClient
        | TogetherClient
        | GroqClient
        | CohereClient
        | OllamaClient
        | BedrockClient
    ),
    wrapper: OpenAIWrapper,
    init_args: dict[str, Any],
) -> None:
    """Log a new client instance."""
    thread_id = threading.get_ident()

    try:
        log_data = json.dumps({
            "client_id": id(client),
            "wrapper_id": id(wrapper),
            "session_id": self.session_id,
            "class": type(client).__name__,
            "json_state": json.dumps(init_args),
            "timestamp": get_current_ts(),
            "thread_id": thread_id,
        })
        self.logger.info(log_data)
    except Exception as e:
        self.logger.error(f"[file_logger] Failed to log event {e}")

log_function_use #

log_function_use(source, function, args, returns)

Log a registered function(can be a tool) use from an agent or a string source.

Source code in autogen/logger/file_logger.py
def log_function_use(self, source: str | Agent, function: F, args: dict[str, Any], returns: Any) -> None:
    """Log a registered function(can be a tool) use from an agent or a string source."""
    thread_id = threading.get_ident()

    try:
        log_data = json.dumps({
            "source_id": id(source),
            "source_name": str(source.name) if hasattr(source, "name") else source,
            "agent_module": source.__module__,
            "agent_class": source.__class__.__name__,
            "timestamp": get_current_ts(),
            "thread_id": thread_id,
            "input_args": safe_serialize(args),
            "returns": safe_serialize(returns),
        })
        self.logger.info(log_data)
    except Exception as e:
        self.logger.error(f"[file_logger] Failed to log event {e}")

get_connection #

get_connection()

Method is intentionally left blank because there is no specific connection needed for the FileLogger.

Source code in autogen/logger/file_logger.py
def get_connection(self) -> None:
    """Method is intentionally left blank because there is no specific connection needed for the FileLogger."""
    pass

stop #

stop()

Close the file handler and remove it from the logger.

Source code in autogen/logger/file_logger.py
def stop(self) -> None:
    """Close the file handler and remove it from the logger."""
    for handler in self.logger.handlers:
        if isinstance(handler, logging.FileHandler):
            handler.close()
            self.logger.removeHandler(handler)