From 646edb7c03ceb93ff0c3d0c095051bd75318daea Mon Sep 17 00:00:00 2001 From: Robin5605 Date: Wed, 17 Jul 2024 20:54:38 -0500 Subject: [PATCH 1/3] Add toggleable JSON logging --- src/mainframe/server.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/mainframe/server.py b/src/mainframe/server.py index 3e61bb92..83fb56d3 100644 --- a/src/mainframe/server.py +++ b/src/mainframe/server.py @@ -49,17 +49,10 @@ def configure_logger(): cache_logger_on_first_use=True, ) - # log_renderer: structlog.types.Processor - # # If running in production, render logs with JSON. - # if mainframe_settings.production: - # log_renderer = structlog.processors.JSONRenderer() - # else: - # # If running in a development environment, pretty print logs - # log_renderer = structlog.dev.ConsoleRenderer() - - # TODO: Once infra for log aggregation is up and running, remove this and go back to - # TODO: JSON logging in production. - log_renderer = structlog.dev.ConsoleRenderer(colors=False) + if GIT_SHA in ("development", "testing"): + log_renderer = structlog.dev.ConsoleRenderer(colors=False) + else: + log_renderer = structlog.processors.JSONRenderer() formatter = structlog.stdlib.ProcessorFormatter( foreign_pre_chain=shared_processors, From 1ccfab588b87ac35370a659f3a4813ed4a162397 Mon Sep 17 00:00:00 2001 From: Robin5605 Date: Thu, 18 Jul 2024 15:12:08 -0500 Subject: [PATCH 2/3] Add option to enable JSON logging Add an option in `constants.py` to enable JSON logging. This will be set to False by default, in the philosophy that future settings similar to this will have development-centric defaults. We would rather have pretty color logging work out of the box in development, and go through the trouble of adding a few flags in production, than have the developer have to add those options in every time locally. --- src/mainframe/constants.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mainframe/constants.py b/src/mainframe/constants.py index 35eb4384..fbd42aee 100644 --- a/src/mainframe/constants.py +++ b/src/mainframe/constants.py @@ -34,6 +34,8 @@ class Mainframe(EnvConfig): job_timeout: int = 60 * 2 + enable_json_logging: bool = False + mainframe_settings = Mainframe() # pyright: ignore From b21df78d1245218b4fed5c5f15ffa668d462435e Mon Sep 17 00:00:00 2001 From: Robin5605 Date: Thu, 18 Jul 2024 15:12:44 -0500 Subject: [PATCH 3/3] Use JSON logging configuration option instead of GIT_SHA Use JSON logging config option instead of GIT_SHA environment variable when determining if we should use structlog's JSON processor or not. --- src/mainframe/server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mainframe/server.py b/src/mainframe/server.py index 83fb56d3..8021e78a 100644 --- a/src/mainframe/server.py +++ b/src/mainframe/server.py @@ -13,7 +13,7 @@ from sentry_sdk.integrations.logging import LoggingIntegration from structlog_sentry import SentryProcessor -from mainframe.constants import GIT_SHA, Sentry +from mainframe.constants import GIT_SHA, Sentry, mainframe_settings from mainframe.dependencies import validate_token, validate_token_override from mainframe.endpoints import routers from mainframe.models.schemas import ServerMetadata @@ -49,10 +49,10 @@ def configure_logger(): cache_logger_on_first_use=True, ) - if GIT_SHA in ("development", "testing"): - log_renderer = structlog.dev.ConsoleRenderer(colors=False) - else: + if mainframe_settings.enable_json_logging is True: log_renderer = structlog.processors.JSONRenderer() + else: + log_renderer = structlog.dev.ConsoleRenderer(colors=False) formatter = structlog.stdlib.ProcessorFormatter( foreign_pre_chain=shared_processors,