Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: feature flag to enable switching between logger for libaries #3161

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved

// Attempt to connect to the local docker daemon.
cli, err := client.NewClientWithOpts(client.FromEnv)
Expand Down
9 changes: 8 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,13 @@ func GetZarfVariableConfig(ctx context.Context) *variables.VariableConfig {
return interactive.PromptVariable(variable)
}

return variables.New("zarf", prompt, logger.From(ctx))
var l *slog.Logger
if logger.Enabled(ctx) {
l = logger.From(ctx)
} else {
l = slog.New(message.ZarfHandler{})
}
return variables.New("zarf", prompt, l)
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
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 {
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
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))
})
}