From 75df24feb2f66b8a27703362dab96a3961a4d276 Mon Sep 17 00:00:00 2001 From: Matteo Gastaldello Date: Wed, 10 Jun 2026 09:51:56 +0200 Subject: [PATCH] feat: emit JSON logs for logs-ingester compatibility Replace prettylog (colored text) with a slog JSON handler on stderr. Each line is now a single JSON object with a canonical "timestamp" in RFC3339Nano UTC plus a "service" field; traceId/name/namespace are already injected by unstructured-runtime. Handler logic inlined to avoid a plumbing bump that conflicts with the runtime's slogs/pretty test dep. --- main.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 6cee56d..2bd755e 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,6 @@ import ( "github.com/krateoplatformops/plumbing/kubeutil/event" "github.com/krateoplatformops/plumbing/kubeutil/eventrecorder" "github.com/krateoplatformops/plumbing/ptr" - prettylog "github.com/krateoplatformops/plumbing/slogs/pretty" "github.com/krateoplatformops/unstructured-runtime/pkg/logging" "github.com/krateoplatformops/unstructured-runtime/pkg/pluralizer" @@ -102,16 +101,22 @@ func main() { logLevel = slog.LevelDebug } - lh := prettylog.New(&slog.HandlerOptions{ + // JSON logs on stderr, compatible with logs-ingester: each line is a single JSON + // object with a canonical "timestamp" field in RFC3339Nano UTC. See + // docs/logs-ingester-compatibility.md. + lh := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ Level: logLevel, AddSource: false, - }, - prettylog.WithDestinationWriter(os.Stderr), - prettylog.WithColor(), - prettylog.WithOutputEmptyAttrs(), - ) + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + if a.Key == slog.TimeKey { + return slog.String("timestamp", a.Value.Time().UTC().Format(time.RFC3339Nano)) + } + return a + }, + }) + sl := slog.New(lh).With(slog.String("service", serviceName)) - log := logging.NewLogrLogger(logr.FromSlogHandler(slog.New(lh).Handler())) + log := logging.NewLogrLogger(logr.FromSlogHandler(sl.Handler())) // Kubernetes configuration var cfg *rest.Config