From e0a08ccf1e89cc4ffa66f45e618235ed11da0ac7 Mon Sep 17 00:00:00 2001 From: Supul Date: Thu, 4 Jun 2026 23:34:25 +0530 Subject: [PATCH] feat(logging): enhance ECSFormatter to use service_name from env var and improve log formatting consistency --- litellm/_logging.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/litellm/_logging.py b/litellm/_logging.py index 7f21b1f2da4..a9d31b96a29 100644 --- a/litellm/_logging.py +++ b/litellm/_logging.py @@ -216,10 +216,12 @@ class ECSFormatter(Formatter): def __init__( self, - service_name: str = os.getenv("LITELLM_SERVICE_NAME", "litellm"), + service_name: Optional[str] = None, ): super().__init__() - self._service_name = service_name + self._service_name = service_name or os.getenv( + "LITELLM_SERVICE_NAME", "litellm" + ) def formatTime( self, record: logging.LogRecord, datefmt: Optional[str] = None @@ -397,12 +399,17 @@ def _initialize_loggers_with_handler(handler: logging.Handler): def _get_uvicorn_json_log_config(): """ - Generate a uvicorn log_config dictionary that applies JSON formatting to all loggers. + Generate a uvicorn log_config dictionary that applies structured formatting to all loggers. - This ensures that uvicorn's access logs, error logs, and all application logs - are formatted as JSON when json_logs is enabled. + Uses ECSFormatter when LITELLM_ECS_LOGS=true so uvicorn access/error logs + are consistent with application logs for downstream ECS consumers. + Falls back to JsonFormatter when only JSON_LOGS=true. """ - json_formatter_class = "litellm._logging.JsonFormatter" + formatter_class = ( + "litellm._logging.ECSFormatter" + if ecs_logs + else "litellm._logging.JsonFormatter" + ) # Use the module-level log_level variable for consistency uvicorn_log_level = log_level.upper() @@ -412,13 +419,13 @@ def _get_uvicorn_json_log_config(): "disable_existing_loggers": False, "formatters": { "json": { - "()": json_formatter_class, + "()": formatter_class, }, "default": { - "()": json_formatter_class, + "()": formatter_class, }, "access": { - "()": json_formatter_class, + "()": formatter_class, }, }, "handlers": {