Skip to content

Commit

Permalink
Improve log level printing (#19030)
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel authored Nov 7, 2023
1 parent 7dfb884 commit 1b90b8a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions components/local-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1b90b8a

Please sign in to comment.