From 1b90b8ac39706735fdf08ee12d2f470ec0fd6da4 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Tue, 7 Nov 2023 21:48:45 +0100 Subject: [PATCH] Improve log level printing (#19030) --- components/local-app/cmd/root.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/components/local-app/cmd/root.go b/components/local-app/cmd/root.go index d8b0e1a8d77972..8858f2bd498bcb 100644 --- a/components/local-app/cmd/root.go +++ b/components/local-app/cmd/root.go @@ -55,11 +55,29 @@ var rootCmd = &cobra.Command{ var noColor bool if !isatty.IsTerminal(os.Stdout.Fd()) { noColor = true + color.Disable() } slog.SetDefault(slog.New(tint.NewHandler(os.Stdout, &tint.Options{ Level: level, NoColor: noColor, TimeFormat: time.StampMilli, + ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr { + if attr.Key != "level" { + return attr + } + + switch attr.Value.String() { + case slog.LevelDebug.String(): + attr.Value = slog.StringValue(color.Gray.Render("[DEBUG]")) + case slog.LevelInfo.String(): + attr.Value = slog.StringValue(color.Green.Render("[INFO ]")) + case slog.LevelWarn.String(): + attr.Value = slog.StringValue(color.Yellow.Render("[WARN ]")) + case slog.LevelError.String(): + attr.Value = slog.StringValue(color.Red.Render("[ERROR]")) + } + return attr + }, }))) cfg, err := config.LoadConfig(rootOpts.ConfigLocation)