From fb5dc027639c9e9638b3b7f416c4cb5a5bc01629 Mon Sep 17 00:00:00 2001 From: Le Quang Tho <92069270+letho1608@users.noreply.github.com> Date: Sat, 28 Mar 2026 09:08:52 +0700 Subject: [PATCH] refactor(robusta): add optional json logging support to robusta runner The runner currently uses the default Python logging format. To integrate with Kibana/ELK stacks we need a way to switch the root logger (and any child loggers) to emit JSON instead of plain text. This should be opt-in via an environment variable so existing deployments are unaffected. Affected files: process_setup.py Signed-off-by: Le Quang Tho <92069270+letho1608@users.noreply.github.com> --- src/robusta/runner/process_setup.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/robusta/runner/process_setup.py b/src/robusta/runner/process_setup.py index 564d3a188..d7dd13d26 100644 --- a/src/robusta/runner/process_setup.py +++ b/src/robusta/runner/process_setup.py @@ -5,6 +5,8 @@ from functools import lru_cache from robusta.core.model.env_vars import RUN_AS_SUBPROCESS +JSON_LOGGING = os.environ.get("ROBUSTA_JSON_LOGGING", "").lower() in ("true", "1", "yes") + def process_setup(): # Cache getLogger calls as we are seeing kubernetes-client locking on logging causing event processing to be extremely delayed @@ -13,6 +15,14 @@ def process_setup(): # Inspiration from Yelp - https://github.com/Yelp/Tron/blob/36337d92fa92bba3da8c5fcc65235697d009eb36/tron/trondaemon.py#L58 logging.getLogger = lru_cache(maxsize=None)(logging.getLogger) + if JSON_LOGGING: + from pythonjsonlogger import jsonlogger + root_logger = logging.getLogger() + handler = logging.StreamHandler() + formatter = jsonlogger.JsonFormatter() + handler.setFormatter(formatter) + root_logger.handlers = [handler] + if RUN_AS_SUBPROCESS: if os.fork(): # Parent process, pid 1 in our deployment scenario. Wait (blocking - doesn't