Skip to content

Commit

Permalink
fix(plugins): improve logging (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzduchardt authored Dec 26, 2023
1 parent e9fe05e commit ba31a3b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions cmd/smart-mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"os"
"strings"

"github.com/logrusorgru/aurora/v4"
"github.com/mykso/myks/internal/myks"
aurora "github.com/logrusorgru/aurora/v4"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/mykso/myks/internal/myks"
)

const (
Expand Down
29 changes: 21 additions & 8 deletions internal/myks/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package myks

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand All @@ -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, "Plugin execution succeeded."))
}
}
return err
}
Expand Down

0 comments on commit ba31a3b

Please sign in to comment.