[chore] Update log.AddSink docs to clarify confusing behavior

This commit is contained in:
Miccah Castorina
2025-12-02 17:56:12 -08:00
parent 821e8b9e5c
commit 79a73b78db
2 changed files with 25 additions and 1 deletions
+4 -1
View File
@@ -137,7 +137,10 @@ func AddSentry(l logr.Logger, opts sentry.ClientOptions, tags map[string]string)
}
// AddSink extends an existing logr.Logger with a new sink. It returns the new
// logr.Logger, a cleanup function, and an error.
// logr.Logger, a cleanup function, and an error. Note that values added to the
// existing logr.Logger via [logr.WithValues] before calling this function will
// not be propogated to the new sink, but they will continue to be written to
// the existing sink.
func AddSink(l logr.Logger, sink logConfig) (logr.Logger, func() error, error) {
if sink.err != nil {
return l, nil, sink.err
+21
View File
@@ -163,6 +163,27 @@ func TestAddSink(t *testing.T) {
assert.Contains(t, buf2.String(), "line 2")
}
// TestAddSinkDoesNotPropogateValues is a pinning test to document that the
// existing functionality of AddSink will not propogate values added to the
// logger.
func TestAddSinkDoesNotPropogateValues(t *testing.T) {
var buf1, buf2 bytes.Buffer
logger, _ := New("service-name",
WithConsoleSink(&buf1),
)
logger = logger.WithValues("foo", "bar")
logger.Info("line 1")
logger, flush, err := AddSink(logger, WithConsoleSink(&buf2))
assert.Nil(t, err)
logger.Info("line 2")
assert.Nil(t, flush())
bufLines1 := strings.Split(buf1.String(), "\n")
assert.Contains(t, bufLines1[0], "foo") // line1 contains "foo"
assert.Contains(t, bufLines1[1], "foo") // line2 contains "foo"
assert.NotContains(t, buf2.String(), "foo") // "foo" not found anywhere in the added sink
}
func TestStaticLevelSink(t *testing.T) {
var buf1, buf2 bytes.Buffer
l1 := zap.NewAtomicLevel()