feat(logging): JSON log config for Uvicorn when LOG_JSON=1 (#11264)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Graham Neubig
2025-10-08 11:26:45 -04:00
committed by GitHub
parent f3c6fd2122
commit 843cc00e79
7 changed files with 123 additions and 39 deletions

View File

@@ -4,7 +4,6 @@ import asyncio
import copy
import os
import time
import traceback
from typing import TYPE_CHECKING, Callable
if TYPE_CHECKING:
@@ -285,19 +284,28 @@ class AgentController:
)
self._closed = True
def log(self, level: str, message: str, extra: dict | None = None) -> None:
def log(
self,
level: str,
message: str,
extra: dict | None = None,
exc_info: bool = False,
) -> None:
"""Logs a message to the agent controller's logger.
Args:
level (str): The logging level to use (e.g., 'info', 'debug', 'error').
message (str): The message to log.
extra (dict | None, optional): Additional fields to log. Includes session_id by default.
exc_info (bool, optional): Whether to include exception info. Defaults to False.
"""
message = f'[Agent Controller {self.id}] {message}'
if extra is None:
extra = {}
extra_merged = {'session_id': self.id, **extra}
getattr(logger, level)(message, extra=extra_merged, stacklevel=2)
getattr(logger, level)(
message, extra=extra_merged, exc_info=exc_info, stacklevel=2
)
async def _react_to_exception(
self,
@@ -364,8 +372,8 @@ class AgentController:
except Exception as e:
self.log(
'error',
f'Error while running the agent (session ID: {self.id}): {e}. '
f'Traceback: {traceback.format_exc()}',
f'Error while running the agent (session ID: {self.id}): {e}',
exc_info=True,
)
reported = RuntimeError(
f'There was an unexpected error while running the agent: {e.__class__.__name__}. You can refresh the page or ask the agent to try again.'