Skip to content

Ensure LogEntrySetField(s) affect value returned by LogEntry #48

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions httplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func LogEntry(ctx context.Context) *slog.Logger {

func LogEntrySetField(ctx context.Context, key string, value slog.Value) {
if entry, ok := ctx.Value(middleware.LogEntryCtxKey).(*RequestLoggerEntry); ok {
entry.Logger = entry.Logger.With(slog.Attr{Key: key, Value: value})
*entry.Logger = *entry.Logger.With(slog.Attr{Key: key, Value: value})
}
}

Expand All @@ -376,6 +376,6 @@ func LogEntrySetFields(ctx context.Context, fields map[string]interface{}) {
attrs[i] = slog.Attr{Key: k, Value: slog.AnyValue(v)}
i++
}
entry.Logger = entry.Logger.With(attrs...)
*entry.Logger = *entry.Logger.With(attrs...)
}
}
38 changes: 24 additions & 14 deletions httplog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,33 @@ func TestLogEntrySetFields(t *testing.T) {
Logger: slog.New(tt.args.handler),
}
req := middleware.WithLogEntry(httptest.NewRequest("GET", "/", nil), entry)
log := LogEntry(req.Context())
// Set fields
LogEntrySetFields(req.Context(), tt.args.fields)

if len(tt.args.handler.attrs) != len(tt.args.fields) {
t.Fatalf("expected %v, got %v", len(tt.args.handler.attrs), len(tt.args.fields))
// Ensure all fields are present in the current handler of LogEntry
logh := log.Handler().(*testHandler)
if len(logh.attrs) != len(tt.args.fields) {
t.Fatalf("expected %v, got %v", len(logh.attrs), len(tt.args.fields))
}
// Ensure all fields are present in the current handler of RequestLoggerEntry
entryh := entry.Logger.Handler().(*testHandler)
if len(entryh.attrs) != len(tt.args.fields) {
t.Fatalf("expected %v, got %v", len(entryh.attrs), len(tt.args.fields))
}
// Ensure all fields are present in the handler
// Iterate over all fields and ensure they are present in both handlers
for k, v := range tt.args.fields {
for i, attr := range tt.args.handler.attrs {
if attr.Key == k {
if !attr.Value.Equal(slog.AnyValue(v)) {
t.Fatalf("expected %v, got %v", attr.Value, v)
for _, logger := range []*slog.Logger{log, entry.Logger} {
handler := logger.Handler().(*testHandler)
for i, attr := range handler.attrs {
if attr.Key == k {
if !attr.Value.Equal(slog.AnyValue(v)) {
t.Fatalf("expected %v, got %v", attr.Value, v)
}
break
}
if i == len(handler.attrs)-1 {
t.Fatalf("expected %v, got %v", k, attr.Key)
}
break
}
if i == len(tt.args.handler.attrs)-1 {
t.Fatalf("expected %v, got %v", k, attr.Key)
}
}
}
Expand All @@ -82,8 +93,7 @@ func (*testHandler) Enabled(_ context.Context, l slog.Level) bool { return true
func (h *testHandler) Handle(ctx context.Context, r slog.Record) error { return nil }

func (h *testHandler) WithAttrs(as []slog.Attr) slog.Handler {
h.attrs = as
return h
return &testHandler{attrs: as}
}

func (h *testHandler) WithGroup(name string) slog.Handler { return h }