diff --git a/exp/zapslog/handler.go b/exp/zapslog/handler.go index 84f43539e..6445aca5f 100644 --- a/exp/zapslog/handler.go +++ b/exp/zapslog/handler.go @@ -197,7 +197,7 @@ func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { func (h *Handler) WithGroup(group string) slog.Handler { cloned := *h if h.holdGroup != "" { - cloned.core = h.core.With([]zapcore.Field{zap.Namespace(group)}) + cloned.core = h.core.With([]zapcore.Field{zap.Namespace(h.holdGroup)}) } cloned.holdGroup = group return &cloned diff --git a/exp/zapslog/handler_test.go b/exp/zapslog/handler_test.go index 67005d04a..67f41c776 100644 --- a/exp/zapslog/handler_test.go +++ b/exp/zapslog/handler_test.go @@ -190,6 +190,23 @@ func TestInlineGroup(t *testing.T) { func TestWithGroup(t *testing.T) { t.Parallel() fac, observedLogs := observer.New(zapcore.DebugLevel) + t.Run("group-in-group", func(t *testing.T) { + sl := slog.New(NewHandler(fac)) + sl.With("a", "b").WithGroup("G").WithGroup("in").Info("msg", "c", "d") + + logs := observedLogs.TakeAll() + require.Len(t, logs, 1, "Expected exactly one entry to be logged") + entry := logs[0] + assert.Equal(t, "", entry.LoggerName, "Unexpected logger name") + assert.Equal(t, map[string]any{ + "G": map[string]any{ + "in": map[string]any{ + "c": "d", + }, + }, + "a": "b", + }, logs[0].ContextMap(), "Unexpected context") + }) t.Run("empty-group-record", func(t *testing.T) { sl := slog.New(NewHandler(fac)) sl.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg")