From e58391dd7d18f1e98e236a7f3f262b1ca5c1123b Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Thu, 5 Mar 2026 14:24:12 -0500 Subject: [PATCH] Add --json-log flag to enable structured JSON logging (#19968) Co-authored-by: github_username Co-authored-by: Claude Opus 4.6 (1M context) --- sgl-model-gateway/bindings/python/src/lib.rs | 5 +++++ .../bindings/python/src/sglang_router/router_args.py | 6 ++++++ sgl-model-gateway/src/main.rs | 5 +++++ sgl-model-gateway/src/server.rs | 3 ++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sgl-model-gateway/bindings/python/src/lib.rs b/sgl-model-gateway/bindings/python/src/lib.rs index e10d602b2..a45a52273 100644 --- a/sgl-model-gateway/bindings/python/src/lib.rs +++ b/sgl-model-gateway/bindings/python/src/lib.rs @@ -354,6 +354,7 @@ struct Router { api_key: Option, log_dir: Option, log_level: Option, + json_log: bool, service_discovery: bool, selector: HashMap, service_discovery_port: u16, @@ -657,6 +658,7 @@ impl Router { api_key = None, log_dir = None, log_level = None, + json_log = false, service_discovery = false, selector = HashMap::new(), service_discovery_port = 80, @@ -743,6 +745,7 @@ impl Router { api_key: Option, log_dir: Option, log_level: Option, + json_log: bool, service_discovery: bool, selector: HashMap, service_discovery_port: u16, @@ -842,6 +845,7 @@ impl Router { api_key, log_dir, log_level, + json_log, service_discovery, selector, service_discovery_port, @@ -963,6 +967,7 @@ impl Router { max_payload_size: self.max_payload_size, log_dir: self.log_dir.clone(), log_level: self.log_level.clone(), + json_log: self.json_log, service_discovery_config, prometheus_config, request_timeout_secs: self.request_timeout_secs, diff --git a/sgl-model-gateway/bindings/python/src/sglang_router/router_args.py b/sgl-model-gateway/bindings/python/src/sglang_router/router_args.py index fecb64cf4..034b4c8ef 100644 --- a/sgl-model-gateway/bindings/python/src/sglang_router/router_args.py +++ b/sgl-model-gateway/bindings/python/src/sglang_router/router_args.py @@ -45,6 +45,7 @@ class RouterArgs: api_key: Optional[str] = None log_dir: Optional[str] = None log_level: Optional[str] = None + json_log: bool = False # Service discovery configuration service_discovery: bool = False selector: Dict[str, str] = dataclasses.field(default_factory=dict) @@ -413,6 +414,11 @@ class RouterArgs: choices=["debug", "info", "warn", "error"], help="Set the logging level. If not specified, defaults to INFO.", ) + logging_group.add_argument( + f"--{prefix}json-log", + action="store_true", + help="Enable structured JSON log output instead of plain text.", + ) # Service discovery configuration k8s_group.add_argument( diff --git a/sgl-model-gateway/src/main.rs b/sgl-model-gateway/src/main.rs index a04e10f05..3d8b9842f 100644 --- a/sgl-model-gateway/src/main.rs +++ b/sgl-model-gateway/src/main.rs @@ -260,6 +260,10 @@ struct CliArgs { #[arg(long, default_value = "info", value_parser = ["debug", "info", "warn", "error"], help_heading = "Logging")] log_level: String, + /// Enable structured JSON log output instead of plain text + #[arg(long, default_value_t = false, help_heading = "Logging")] + json_log: bool, + // ==================== Prometheus Metrics ==================== /// Port to expose Prometheus metrics #[arg(long, default_value_t = 29000, help_heading = "Prometheus Metrics")] @@ -1119,6 +1123,7 @@ impl CliArgs { max_payload_size: self.max_payload_size, log_dir: self.log_dir.clone(), log_level: Some(self.log_level.clone()), + json_log: self.json_log, service_discovery_config, prometheus_config, request_timeout_secs: self.request_timeout_secs, diff --git a/sgl-model-gateway/src/server.rs b/sgl-model-gateway/src/server.rs index fd762c66e..4cea623de 100644 --- a/sgl-model-gateway/src/server.rs +++ b/sgl-model-gateway/src/server.rs @@ -533,6 +533,7 @@ pub struct ServerConfig { pub max_payload_size: usize, pub log_dir: Option, pub log_level: Option, + pub json_log: bool, pub service_discovery_config: Option, pub prometheus_config: Option, pub request_timeout_secs: u64, @@ -722,7 +723,7 @@ pub async fn startup(config: ServerConfig) -> Result<(), Box