From d9b52b3a4918ea8b008cb3c8207424a90773f704 Mon Sep 17 00:00:00 2001 From: Fritz Duchardt Date: Tue, 26 Dec 2023 09:22:23 +0100 Subject: [PATCH] Tweak logging for plugin execution (once more) --- cmd/root.go | 1 + internal/myks/plugins.go | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f9f8f024..9e80fa9c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,6 +28,7 @@ var ( func NewMyksCmd(version, commit, date string) *cobra.Command { cobra.OnInitialize(initLogger) + zerolog.SetGlobalLevel(zerolog.InfoLevel) cmd := newRootCmd(version, commit, date) cmd.AddCommand(allCmd) cmd.AddCommand(renderCmd) diff --git a/internal/myks/plugins.go b/internal/myks/plugins.go index fa5e5d44..c0f7b801 100644 --- a/internal/myks/plugins.go +++ b/internal/myks/plugins.go @@ -2,6 +2,7 @@ package myks import ( "bytes" + "fmt" "os" "os/exec" "path/filepath" @@ -96,7 +97,7 @@ func (p PluginCmd) Name() string { } func (p PluginCmd) Exec(a *Application, args []string) error { - step := "Plugin " + p.Name() + step := p.Name() log.Trace().Msg(a.Msg(step, "execution started")) env, err := p.generateEnv(a) @@ -114,16 +115,28 @@ func (p PluginCmd) Exec(a *Application, args []string) error { // log env log.Debug().Msg(a.Msg(step, msgRunCmd("", p.cmd, args))) err = cmd.Run() + allOutput := stderrBs.String() + stdoutBs.String() if err != nil { log.Error().Msg(msgRunCmd("Failed on step: "+step, p.cmd, args)) - log.Error().Err(err). - Str("stdout", stdoutBs.String()). - // writing std error into message to avoid wrapping - Msg(a.Msg(step, "Plugin execution failed: "+stderrBs.String())) + if allOutput != "" { + log.Error().Err(err). + // writing std error into message to avoid wrapping + Msg(a.Msg(step, fmt.Sprintf("Plugin execution failed:\n\n%s", allOutput))) + } else { + log.Error().Err(err). + // writing std error into message to avoid wrapping + Msg(a.Msg(step, "Plugin execution failed")) + } } else { - log.Info(). - // writing std out into message to avoid wrapping - Msg(a.Msg(step, "Plugin execution succeeded: "+stdoutBs.String())) + if allOutput != "" { + log.Info(). + // writing std out into message to avoid wrapping + Msg(a.Msg(step, fmt.Sprintf("Plugin execution succeeded:\n\n%s", allOutput))) + } else { + log.Info(). + // writing std out into message to avoid wrapping + Msg(a.Msg(step, fmt.Sprintf("Plugin execution succeeded."))) + } } return err }