mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 13:21:01 +00:00
feat(cli): hide redundant log attributes
This commit is contained in:
parent
c9ab40b539
commit
9e654521ae
3 changed files with 63 additions and 15 deletions
28
cmd/root.go
28
cmd/root.go
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue