Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend, libs): Tell uvicorn to use our logger + always log to stdout+stderr (#9742)

Nicholas Tindle7179f9cea0c1
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/logging/config.py b/autogpt_platform/autogpt_libs/autogpt_libs/logging/config.py
index dcbcc6ecf8..dda6c58547 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/logging/config.py
+++ b/autogpt_platform/autogpt_libs/autogpt_libs/logging/config.py
@@ -8,7 +8,7 @@ from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from .filters import BelowLevelFilter
-from .formatters import AGPTFormatter, StructuredLoggingFormatter
+from .formatters import AGPTFormatter
LOG_DIR = Path(__file__).parent.parent.parent.parent / "logs"
LOG_FILE = "activity.log"
@@ -81,9 +81,26 @@ def configure_logging(force_cloud_logging: bool = False) -> None:
"""
config = LoggingConfig()
-
log_handlers: list[logging.Handler] = []
+ # Console output handlers
+ stdout = logging.StreamHandler(stream=sys.stdout)
+ stdout.setLevel(config.level)
+ stdout.addFilter(BelowLevelFilter(logging.WARNING))
+ if config.level == logging.DEBUG:
+ stdout.setFormatter(AGPTFormatter(DEBUG_LOG_FORMAT))
+ else:
+ stdout.setFormatter(AGPTFormatter(SIMPLE_LOG_FORMAT))
+
+ stderr = logging.StreamHandler()
+ stderr.setLevel(logging.WARNING)
+ if config.level == logging.DEBUG:
+ stderr.setFormatter(AGPTFormatter(DEBUG_LOG_FORMAT))
+ else:
+ stderr.setFormatter(AGPTFormatter(SIMPLE_LOG_FORMAT))
+
+ log_handlers += [stdout, stderr]
+
# Cloud logging setup
if config.enable_cloud_logging or force_cloud_logging:
import google.cloud.logging
@@ -97,26 +114,7 @@ def configure_logging(force_cloud_logging: bool = False) -> None:
transport=SyncTransport,
)
cloud_handler.setLevel(config.level)
- cloud_handler.setFormatter(StructuredLoggingFormatter())
log_handlers.append(cloud_handler)
- else:
- # Console output handlers
- stdout = logging.StreamHandler(stream=sys.stdout)
- stdout.setLevel(config.level)
- stdout.addFilter(BelowLevelFilter(logging.WARNING))
- if config.level == logging.DEBUG:
- stdout.setFormatter(AGPTFormatter(DEBUG_LOG_FORMAT))
- else:
- stdout.setFormatter(AGPTFormatter(SIMPLE_LOG_FORMAT))
-
- stderr = logging.StreamHandler()
- stderr.setLevel(logging.WARNING)
- if config.level == logging.DEBUG:
- stderr.setFormatter(AGPTFormatter(DEBUG_LOG_FORMAT))
- else:
- stderr.setFormatter(AGPTFormatter(SIMPLE_LOG_FORMAT))
-
- log_handlers += [stdout, stderr]
# File logging setup
if config.enable_file_logging:
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/logging/formatters.py b/autogpt_platform/autogpt_libs/autogpt_libs/logging/formatters.py
index cf27bc4667..28b998f2ce 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/logging/formatters.py
+++ b/autogpt_platform/autogpt_libs/autogpt_libs/logging/formatters.py
@@ -1,7 +1,6 @@
import logging
from colorama import Fore, Style
-from google.cloud.logging_v2.handlers import CloudLoggingFilter, StructuredLogHandler
from .utils import remove_color_codes
@@ -80,16 +79,3 @@ class AGPTFormatter(FancyConsoleFormatter):
return remove_color_codes(super().format(record))
else:
return super().format(record)
-
-
-class StructuredLoggingFormatter(StructuredLogHandler, logging.Formatter):
- def __init__(self):
- # Set up CloudLoggingFilter to add diagnostic info to the log records
- self.cloud_logging_filter = CloudLoggingFilter()
-
- # Init StructuredLogHandler
- super().__init__()
-
- def format(self, record: logging.LogRecord) -> str:
- self.cloud_logging_filter.filter(record)
- return super().format(record)
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/logging/utils.py b/autogpt_platform/autogpt_libs/autogpt_libs/logging/utils.py
index 5c5c09221c..4d7481da51 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/logging/utils.py
+++ b/autogpt_platform/autogpt_libs/autogpt_libs/logging/utils.py
@@ -1,7 +1,7 @@
import logging
import re
from typing import Any
-
+import uvicorn.config
from colorama import Fore
@@ -25,3 +25,14 @@ def print_attribute(
"color": value_color,
},
)
+
+
+def generate_uvicorn_config():
+ """
+ Generates a uvicorn logging config that silences uvicorn's default logging and tells it to use the native logging module.
+ """
+ log_config = dict(uvicorn.config.LOGGING_CONFIG)
+ log_config["loggers"]["uvicorn"] = {"handlers": []}
+ log_config["loggers"]["uvicorn.error"] = {"handlers": []}
+ log_config["loggers"]["uvicorn.access"] = {"handlers": []}
+ return log_config
diff --git a/autogpt_platform/backend/backend/server/rest_api.py b/autogpt_platform/backend/backend/server/rest_api.py
index b1c1dcfbf5..dafe5d9871 100644
--- a/autogpt_platform/backend/backend/server/rest_api.py
+++ b/autogpt_platform/backend/backend/server/rest_api.py
@@ -3,6 +3,7 @@ import logging
from typing import Any, Optional
import autogpt_libs.auth.models
+from autogpt_libs.logging.utils import generate_uvicorn_config
import fastapi
import fastapi.responses
import starlette.middleware.cors
@@ -141,6 +142,7 @@ class AgentServer(backend.util.service.AppProcess):
server_app,
host=backend.util.settings.Config().agent_api_host,
port=backend.util.settings.Config().agent_api_port,
+ log_config=generate_uvicorn_config(),
)
@staticmethod
diff --git a/autogpt_platform/backend/backend/server/ws_api.py b/autogpt_platform/backend/backend/server/ws_api.py
index 56d16ab1ef..d98632cb05 100644
--- a/autogpt_platform/backend/backend/server/ws_api.py
+++ b/autogpt_platform/backend/backend/server/ws_api.py
@@ -3,6 +3,7 @@ import logging
from contextlib import asynccontextmanager
from typing import Protocol
+from autogpt_libs.logging.utils import generate_uvicorn_config
import uvicorn
from autogpt_libs.auth import parse_jwt_token
from autogpt_libs.utils.cache import thread_cached
@@ -286,8 +287,10 @@ class WebsocketServer(AppProcess):
allow_methods=["*"],
allow_headers=["*"],
)
+
uvicorn.run(
server_app,
host=Config().websocket_server_host,
port=Config().websocket_server_port,
+ log_config=generate_uvicorn_config(),
)