Skip to content

Commit 1b90b8a

Browse files
authored
Improve log level printing (#19030)
1 parent 7dfb884 commit 1b90b8a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

components/local-app/cmd/root.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,29 @@ var rootCmd = &cobra.Command{
5555
var noColor bool
5656
if !isatty.IsTerminal(os.Stdout.Fd()) {
5757
noColor = true
58+
color.Disable()
5859
}
5960
slog.SetDefault(slog.New(tint.NewHandler(os.Stdout, &tint.Options{
6061
Level: level,
6162
NoColor: noColor,
6263
TimeFormat: time.StampMilli,
64+
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
65+
if attr.Key != "level" {
66+
return attr
67+
}
68+
69+
switch attr.Value.String() {
70+
case slog.LevelDebug.String():
71+
attr.Value = slog.StringValue(color.Gray.Render("[DEBUG]"))
72+
case slog.LevelInfo.String():
73+
attr.Value = slog.StringValue(color.Green.Render("[INFO ]"))
74+
case slog.LevelWarn.String():
75+
attr.Value = slog.StringValue(color.Yellow.Render("[WARN ]"))
76+
case slog.LevelError.String():
77+
attr.Value = slog.StringValue(color.Red.Render("[ERROR]"))
78+
}
79+
return attr
80+
},
6381
})))
6482

6583
cfg, err := config.LoadConfig(rootOpts.ConfigLocation)

0 commit comments

Comments
 (0)