Skip to content

Improve log level printing #19030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ]"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that we're keeping the indentation of the messages consistent with this, although it may be a bit confusing for grepping output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also looks a bit odd if there isn't any output that actually aligns here.
Let's run with it for now and change it if it's too odd.

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