From a2ff3e0bdb4338b55630d31bff2bafb52d5a99a0 Mon Sep 17 00:00:00 2001 From: Neha Sherpa Date: Wed, 11 Feb 2026 16:54:49 -0800 Subject: [PATCH] fix: Add configurable field name remapping for JSON logging Add a remap config option to allow field name remapping in JSON mode, enabling proper log formatting for different log aggregation systems. Example configuration for Datadog compatibility: ```hcl log { json = true remap = { msg = "message" time = "timestamp" } } ``` This allows Datadog to correctly extract the log message as content instead of displaying the entire JSON object. --- internal/logging/logging.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 5158730..b043147 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -10,8 +10,9 @@ import ( ) type Config struct { - JSON bool `hcl:"json,optional" help:"Enable JSON logging."` - Level slog.Level `hcl:"level" help:"Set the logging level." default:"info"` + JSON bool `hcl:"json,optional" help:"Enable JSON logging."` + Level slog.Level `hcl:"level" help:"Set the logging level." default:"info"` + Remap map[string]string `hcl:"remap,optional" help:"Remap field names from old to new (e.g., msg=message, time=timestamp)."` } type logKey struct{} @@ -19,7 +20,19 @@ type logKey struct{} func Configure(ctx context.Context, config Config) (*slog.Logger, context.Context) { var handler slog.Handler if config.JSON { - handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: config.Level}) + options := &slog.HandlerOptions{Level: config.Level} + if len(config.Remap) > 0 { + options.ReplaceAttr = func(groups []string, a slog.Attr) slog.Attr { + if len(groups) > 0 { + return a + } + if newName, ok := config.Remap[a.Key]; ok { + a.Key = newName + } + return a + } + } + handler = slog.NewJSONHandler(os.Stdout, options) } else { handler = tint.NewHandler(os.Stderr, &tint.Options{ Level: config.Level,