fix(logging): propagate bound slog attrs to wrapped handler in FeedHandler

FeedHandler.WithAttrs/WithGroup only tracked bound attrs for the live
feed copy; Handle forwarded the bare record to the wrapped handler, so
component (and any other With-bound attr) never reached stderr/file
JSON sinks or survived restart for historical log filtering. Now
WithAttrs/WithGroup also derive the wrapped handler via its own
With*, matching agentexec.SystemLogHandler's existing pattern.
This commit is contained in:
sam committed 2026-09-02 19:45:42 +10:00
1 parent e96721ba68
commit de3cd32c72
3 files changed
+26 -1

No files matched your search

+2
View File
@@ -108,12 +108,14 @@ func (h *FeedHandler) Handle(ctx context.Context, record slog.Record) error {
func (h *FeedHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
clone := *h
clone.attrs = append(append([]slog.Attr(nil), h.attrs...), PrefixAttrs(attrs, h.groups)...)
clone.next = h.next.WithAttrs(attrs)
return &clone
}
func (h *FeedHandler) WithGroup(name string) slog.Handler {
clone := *h
clone.groups = append(append([]string(nil), h.groups...), name)
clone.next = h.next.WithGroup(name)
return &clone
}
+22
View File
@@ -1,7 +1,9 @@
package logging
import (
"bytes"
"context"
"encoding/json"
"log/slog"
"testing"
)
@@ -29,6 +31,26 @@ func TestFeedCapturesStructuredSlogEntriesInOrder(t *testing.T) {
}
}
func TestFeedHandlerWithAttrsPropagatesToWrappedHandler(t *testing.T) {
feed := NewFeed(2)
var buf bytes.Buffer
logger := slog.New(NewFeedHandler(slog.NewJSONHandler(&buf, nil), feed)).With("component", "curator")
logger.Info("ran")
entries := feed.Snapshot()
if len(entries) != 1 || entries[0].Fields["component"] != "curator" {
t.Fatalf("feed entry = %#v, want component=curator", entries)
}
var decoded map[string]any
if err := json.Unmarshal(buf.Bytes(), &decoded); err != nil {
t.Fatalf("json.Unmarshal(persistent sink output) = %v", err)
}
if decoded["component"] != "curator" {
t.Fatalf("persistent sink output = %#v, want component=curator", decoded)
}
}
type testWriter struct{}
func (testWriter) Write(p []byte) (int, error) { return len(p), nil }