Skip to content

Commit

Permalink
feat: make logs prettier, fix a bug with log attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir committed May 11, 2024
1 parent 5115026 commit fc48618
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
13 changes: 8 additions & 5 deletions app/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

Expand Down Expand Up @@ -308,15 +310,16 @@ func buildLogger(flags *flag.FlagSet) (*slog.Logger, error) {
return nil, fmt.Errorf("failed to parse log level: %w", err)
}

handlerOpts := &slog.HandlerOptions{
Level: level,
}
writer := os.Stderr

switch logfmt {
case logFormatJSON:
handler = slog.NewJSONHandler(os.Stderr, handlerOpts)
handler = slog.NewJSONHandler(writer, &slog.HandlerOptions{Level: level})
case logFormatText, "fancy":
handler = slog.NewTextHandler(os.Stderr, handlerOpts)
handler = tint.NewHandler(writer, &tint.Options{
Level: level,
NoColor: !isatty.IsTerminal(writer.Fd()),
})
default:
return nil, fmt.Errorf("unknown log format: %s", logfmt)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.22.3

require (
github.com/hashicorp/go-multierror v1.1.1
github.com/lmittmann/tint v1.0.4
github.com/mattn/go-isatty v0.0.20
github.com/neilotoole/slogt v1.1.0
github.com/schollz/progressbar/v3 v3.14.2
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -85,7 +87,6 @@ require (
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI=
Expand Down
11 changes: 3 additions & 8 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ const (
longDestPvcName = "dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-" +
"dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest-dest"

logConfigCmdline = "--log-level debug --log-format json "
netpolsCmdline = "--helm-set rsync.networkPolicy.enabled=true --helm-set sshd.networkPolicy.enabled=true "

migrateLegacyCmdline = logConfigCmdline + "migrate " + netpolsCmdline

migrateWithoutNetpolsCmdline = logConfigCmdline
migrateCmdline = migrateWithoutNetpolsCmdline + netpolsCmdline
migrateCmdline = "--helm-set rsync.networkPolicy.enabled=true --helm-set sshd.networkPolicy.enabled=true"
migrateLegacyCmdline = "migrate " + migrateCmdline
)

var (
Expand Down Expand Up @@ -319,7 +314,7 @@ func TestFailWithoutNetworkPolicies(t *testing.T) {
_, err := execInPod(ctx, mainClusterCli, ns2, "dest", generateExtraDataShellCommand)
require.NoError(t, err)

cmd := fmt.Sprintf("%s -i -n %s -N %s --source source --dest dest", migrateWithoutNetpolsCmdline, ns1, ns2)
cmd := fmt.Sprintf("--log-level debug --log-format json -i -n %s -N %s --source source --dest dest", ns1, ns2)
require.Error(t, runCliApp(ctx, cmd))
}

Expand Down
12 changes: 6 additions & 6 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func (m *Migrator) Run(ctx context.Context, request *migration.Request, logger *
for _, name := range request.Strategies {
attemptID := util.RandomHexadecimalString(attemptIDLength)

logger = logger.With("attempt_id", attemptID, "strategy", name)
attemptLogger := logger.With("attempt_id", attemptID, "strategy", name)

logger.Info("🚁 Attempt using strategy")
attemptLogger.Info("🚁 Attempt using strategy")

attempt := migration.Attempt{
ID: attemptID,
Expand All @@ -70,20 +70,20 @@ func (m *Migrator) Run(ctx context.Context, request *migration.Request, logger *

s := nameToStrategyMap[name]

if runErr := s.Run(ctx, &attempt, logger); runErr != nil {
if runErr := s.Run(ctx, &attempt, attemptLogger); runErr != nil {
if errors.Is(err, strategy.ErrUnaccepted) {
logger.Info("🦊 This strategy cannot handle this migration, will try the next one")
attemptLogger.Info("🦊 This strategy cannot handle this migration, will try the next one")

continue
}

logger.Warn("🔶 Migration failed with this strategy, "+
attemptLogger.Warn("🔶 Migration failed with this strategy, "+
"will try with the remaining strategies", "error", runErr)

continue
}

logger.Info("✅ Migration succeeded")
attemptLogger.Info("✅ Migration succeeded")

return nil
}
Expand Down

0 comments on commit fc48618

Please sign in to comment.