Skip to content

Commit 80dbb62

Browse files
feat: emit JSON logs for logs-ingester compatibility (#185)
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.
1 parent 4377879 commit 80dbb62

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

main.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/krateoplatformops/plumbing/kubeutil/event"
3030
"github.com/krateoplatformops/plumbing/kubeutil/eventrecorder"
3131
"github.com/krateoplatformops/plumbing/ptr"
32-
prettylog "github.com/krateoplatformops/plumbing/slogs/pretty"
3332

3433
"github.com/krateoplatformops/unstructured-runtime/pkg/logging"
3534
"github.com/krateoplatformops/unstructured-runtime/pkg/pluralizer"
@@ -102,16 +101,22 @@ func main() {
102101
logLevel = slog.LevelDebug
103102
}
104103

105-
lh := prettylog.New(&slog.HandlerOptions{
104+
// JSON logs on stderr, compatible with logs-ingester: each line is a single JSON
105+
// object with a canonical "timestamp" field in RFC3339Nano UTC. See
106+
// docs/logs-ingester-compatibility.md.
107+
lh := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
106108
Level: logLevel,
107109
AddSource: false,
108-
},
109-
prettylog.WithDestinationWriter(os.Stderr),
110-
prettylog.WithColor(),
111-
prettylog.WithOutputEmptyAttrs(),
112-
)
110+
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
111+
if a.Key == slog.TimeKey {
112+
return slog.String("timestamp", a.Value.Time().UTC().Format(time.RFC3339Nano))
113+
}
114+
return a
115+
},
116+
})
117+
sl := slog.New(lh).With(slog.String("service", serviceName))
113118

114-
log := logging.NewLogrLogger(logr.FromSlogHandler(slog.New(lh).Handler()))
119+
log := logging.NewLogrLogger(logr.FromSlogHandler(sl.Handler()))
115120

116121
// Kubernetes configuration
117122
var cfg *rest.Config

0 commit comments

Comments
 (0)