From 295a6e9797638384f0c1cb839398d35236317d42 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Mon, 8 Apr 2024 11:50:48 +0400 Subject: [PATCH] test: comment out slogtest.Run Signed-off-by: knqyf263 --- pkg/log/context.go | 2 +- pkg/log/handler.go | 21 ++++++++++++++------- pkg/log/handler_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/log/logger.go | 12 ++++-------- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/pkg/log/context.go b/pkg/log/context.go index eec6db174c3d..05797cf77207 100644 --- a/pkg/log/context.go +++ b/pkg/log/context.go @@ -40,7 +40,7 @@ func WithContextAttrs(ctx context.Context, attrs ...slog.Attr) context.Context { } func contextualAttrs(ctx context.Context) []slog.Attr { - if attrs, ok := ctx.Value(prefixKey{}).([]slog.Attr); ok { + if attrs, ok := ctx.Value(attrKey{}).([]slog.Attr); ok { return attrs } return nil diff --git a/pkg/log/handler.go b/pkg/log/handler.go index fa0a259c6ad4..2b10b8d5cf0d 100644 --- a/pkg/log/handler.go +++ b/pkg/log/handler.go @@ -129,7 +129,7 @@ func (h *ColorHandler) appendAttr(buf []byte, a slog.Attr, groups []string) []by return buf } -func (h *ColorHandler) Handle(_ context.Context, r slog.Record) error { +func (h *ColorHandler) Handle(ctx context.Context, r slog.Record) error { bufp := allocBuf() buf := *bufp defer func() { @@ -137,7 +137,7 @@ func (h *ColorHandler) Handle(_ context.Context, r slog.Record) error { freeBuf(bufp) }() - buf = h.handle(buf, r) + buf = h.handle(ctx, buf, r) h.mu.Lock() defer h.mu.Unlock() @@ -149,7 +149,7 @@ func (h *ColorHandler) Handle(_ context.Context, r slog.Record) error { return nil } -func (h *ColorHandler) handle(buf []byte, r slog.Record) []byte { +func (h *ColorHandler) handle(ctx context.Context, buf []byte, r slog.Record) []byte { colorize := color.New() switch r.Level { case slog.LevelDebug: @@ -175,7 +175,7 @@ func (h *ColorHandler) handle(buf []byte, r slog.Record) []byte { buf = append(buf, '\t') // Message - buf = append(buf, r.Message...) + buf = append(buf, contextualPrefix(ctx)+r.Message...) if r.Level == LevelFatal { // Show the error and return early. format := lo.Ternary(h.opts.Level == slog.LevelDebug, "\n - %+v\n", "\t%v\n") @@ -183,12 +183,19 @@ func (h *ColorHandler) handle(buf []byte, r slog.Record) []byte { } // Attrs - if len(h.preformatted) > 0 || r.NumAttrs() > 0 { + var preformatted []byte + for _, a := range contextualAttrs(ctx) { + preformatted = h.appendAttr(preformatted, a, h.groups) + preformatted = append(preformatted, ' ') + } + preformatted = append(preformatted, h.preformatted...) + + if len(preformatted) > 0 || r.NumAttrs() > 0 { buf = append(buf, '\t') } - if len(h.preformatted) > 0 { - buf = append(buf, h.preformatted...) + if len(preformatted) > 0 { + buf = append(buf, preformatted...) } r.Attrs(func(a slog.Attr) bool { buf = h.appendAttr(buf, a, h.groups) diff --git a/pkg/log/handler_test.go b/pkg/log/handler_test.go index 5d2dcc7267fb..703294673ea5 100644 --- a/pkg/log/handler_test.go +++ b/pkg/log/handler_test.go @@ -2,6 +2,7 @@ package log_test import ( "bytes" + "context" "errors" "fmt" "github.com/aquasecurity/trivy/pkg/log" @@ -93,6 +94,44 @@ func TestWithAttrsAndWithGroup(t *testing.T) { }) } +func TestContext(t *testing.T) { + t.Run("with context prefix", func(t *testing.T) { + var buf bytes.Buffer + baseLogger := log.New(log.NewHandler(&buf, &log.Options{Level: slog.LevelInfo})) + + // Test logging with WithContextPrefix + ctx := context.Background() + ctx = log.WithContextPrefix(ctx, "prefix1") + + logger := baseLogger.With("key1", "value1").WithGroup("group1") + logger.InfoContext(ctx, "info message", "key2", true) + + got := buf.String() + wantLines := []string{ + `INFO [prefix1] info message key1="value1" group1.key2=true`, + } + compareLines(t, got, wantLines) + }) + + t.Run("with context attrs", func(t *testing.T) { + var buf bytes.Buffer + baseLogger := log.New(log.NewHandler(&buf, &log.Options{Level: slog.LevelInfo})) + + // Test logging with WithContextAttrs + ctx := context.Background() + ctx = log.WithContextAttrs(ctx, log.String("key1", "value1")) + + logger := baseLogger.WithGroup("group1") + logger.InfoContext(ctx, "info message", "key2", true) + + got := buf.String() + wantLines := []string{ + `INFO info message group1.key1="value1" group1.key2=true`, + } + compareLines(t, got, wantLines) + }) +} + func compareLines(t *testing.T, got string, wantLines []string) { // Strip color codes from the output. got = stripColorCodes(got) @@ -126,6 +165,7 @@ func stripColorCodes(s string) string { return s } +// TODO: slogtest.Run was added in Go 1.22. Waiting for https://github.com/aquasecurity/trivy/pull/6075. func TestSlogtest(t *testing.T) { var buf bytes.Buffer newHandler := func(*testing.T) slog.Handler { diff --git a/pkg/log/logger.go b/pkg/log/logger.go index a88ec019f923..77b5c9d13f1b 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -81,26 +81,22 @@ func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ... func (l *Logger) Debug(msg string, args ...any) { l.inner.Debug(l.prefix+msg, args...) } func (l *Logger) Debugf(format string, args ...any) { l.Debug(fmt.Sprintf(format, args...)) } func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any) { - args = append([]any{contextualAttrs(ctx)}, args...) - l.inner.DebugContext(ctx, contextualPrefix(ctx)+msg, args...) + l.inner.DebugContext(ctx, msg, args...) } func (l *Logger) Info(msg string, args ...any) { l.inner.Info(l.prefix+msg, args...) } func (l *Logger) Infof(format string, args ...any) { l.Info(fmt.Sprintf(format, args...)) } func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any) { - args = append([]any{contextualAttrs(ctx)}, args...) - l.inner.InfoContext(ctx, contextualPrefix(ctx)+msg, args...) + l.inner.InfoContext(ctx, msg, args...) } func (l *Logger) Warn(msg string, args ...any) { l.inner.Warn(l.prefix+msg, args...) } func (l *Logger) Warnf(format string, args ...any) { l.Warn(fmt.Sprintf(format, args...)) } func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any) { - args = append([]any{contextualAttrs(ctx)}, args...) - l.inner.WarnContext(ctx, contextualPrefix(ctx)+msg, args...) + l.inner.WarnContext(ctx, msg, args...) } func (l *Logger) Error(msg string, args ...any) { l.inner.Error(l.prefix+msg, args...) } func (l *Logger) Errorf(format string, args ...any) { l.Error(fmt.Sprintf(format, args...)) } func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any) { - args = append([]any{contextualAttrs(ctx)}, args...) - l.inner.ErrorContext(ctx, contextualPrefix(ctx)+msg, args...) + l.inner.ErrorContext(ctx, msg, args...) } func (l *Logger) Write(p []byte) (n int, err error) {