feat(cli): hide redundant log attributes

This commit is contained in:
Julian Tölle 2024-05-04 23:45:58 +02:00
parent c9ab40b539
commit 9e654521ae
3 changed files with 63 additions and 15 deletions

View file

@ -37,13 +37,7 @@ var rootCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
logLevel := slog.LevelInfo
if verbose >= 1 {
logLevel = slog.LevelDebug
}
slog.SetDefault(slog.New(ui.NewHandler(os.Stdout, &ui.HandlerOptions{
Level: logLevel,
})))
slog.SetDefault(initLogger())
// Add logger to command context
logger := slog.Default()
@ -54,6 +48,26 @@ var rootCmd = &cobra.Command{
},
}
func initLogger() *slog.Logger {
logLevel := slog.LevelInfo
if verbose >= 1 {
logLevel = slog.LevelDebug
}
return slog.New(ui.NewHandler(os.Stdout, &ui.HandlerOptions{
Level: logLevel,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove attributes that are unnecessary for the cli context
if a.Key == "library" || a.Key == "method" {
return slog.Attr{}
}
return a
},
}))
}
func newClient(ctx context.Context) *hcloudimages.Client {
logger := contextlogger.From(ctx)
// Build hcloud-go client