Skip to content

Commit

Permalink
chore: feature flag to enable switching between logger for libaries (#…
Browse files Browse the repository at this point in the history
…3161)

Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 authored Oct 29, 2024
1 parent 4b7e6e4 commit 4d78738
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func preRun(cmd *cobra.Command, _ []string) error {
var disableMessage bool
if LogFormat != "" {
disableMessage = true
ctx := logger.WithLoggingEnabled(ctx, true)
cmd.SetContext(ctx)
}
err = setupMessage(messageCfg{
level: LogLevelCLI,
Expand Down
1 change: 1 addition & 0 deletions src/internal/packager/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er

// TODO(mkcp): Remove message on logger release
message.Warnf("Falling back to local 'docker', failed to find the manifest on a remote: %s", err.Error())
l.Warn("Falling back to local 'docker', failed to find the manifest on a remote", "error", err.Error())

// Attempt to connect to the local docker daemon.
cli, err := client.NewClientWithOpts(client.FromEnv)
Expand Down
6 changes: 5 additions & 1 deletion src/internal/packager/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/base64"
"fmt"
"log/slog"
"strings"

"github.com/zarf-dev/zarf/src/api/v1alpha1"
Expand Down Expand Up @@ -35,7 +36,10 @@ func GetZarfVariableConfig(ctx context.Context) *variables.VariableConfig {
return interactive.PromptVariable(variable)
}

return variables.New("zarf", prompt, logger.From(ctx))
if logger.Enabled(ctx) {
return variables.New("zarf", prompt, logger.From(ctx))
}
return variables.New("zarf", prompt, slog.New(message.ZarfHandler{}))
}

// GetZarfTemplates returns the template keys and values to be used for templating.
Expand Down
24 changes: 24 additions & 0 deletions src/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ func WithContext(ctx context.Context, logger *slog.Logger) context.Context {
return context.WithValue(ctx, defaultCtxKey, logger)
}

// TODO (@austinabro321) once we switch over to the new logger completely the enabled key & logic should be deleted
type ctxKeyEnabled struct{}

var defaultCtxKeyEnabled = ctxKeyEnabled{}

// WithLoggingEnabled allows stores a value to determine whether or not slog logging is enabled
func WithLoggingEnabled(ctx context.Context, enabled bool) context.Context {
return context.WithValue(ctx, defaultCtxKeyEnabled, enabled)
}

// Enabled returns true if slog logging is enabled
func Enabled(ctx context.Context) bool {
if ctx == nil {
return false
}
enabled := ctx.Value(defaultCtxKeyEnabled)
switch v := enabled.(type) {
case bool:
return v
default:
return false
}
}

// From takes a context and reads out a *slog.Logger. If From does not find a value it will return a discarding logger
// similar to log-format "none".
func From(ctx context.Context) *slog.Logger {
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,8 @@ func TestContext(t *testing.T) {
res := From(ctx)
require.NotNil(t, res)
})
t.Run("can add a flag to the context to determine if enabled", func(t *testing.T) {
ctx := WithLoggingEnabled(context.Background(), true)
require.True(t, Enabled(ctx))
})
}

0 comments on commit 4d78738

Please sign in to comment.