Make `Named` work with nested loggers
cmoog opened this issue · 4 comments
Similar to the issue with nested loggers not respecting Leveled
, the same problem exists for Named
.
Can you clarify here with a small example? I'm not sure how Named
couldn't work with nested loggers.
The implementation is dead simple, see https://github.com/cdr/slog/blob/5519752b31c469cf4e04c220ec381eea4341f953/slog.go#L129
Ah, so I'd say this is more of a confusion I have with the Make
API... so this issue should probably be closed. Take the example below: what I'm really trying to do is "append" a Sink
to a logger but keep the name, level, etc.
func TestNestedNamed(t *testing.T) {
t.Parallel()
s1 := &fakeSink{}
s2 := &fakeSink{}
l := slog.Make(s1).Named("s1")
l = slog.Make(l, s2).Named("s2")
l.Info(bg, "")
// passes
assert.Len(t, "s1 has 2 names", 2, s1.entries[0].LoggerNames)
// fails
assert.Len(t, "s2 has 2 names", 2, s2.entries[0].LoggerNames)
}
This "append" operation is essentially what func WithSentryIfEnabled(ctx context.Context, log slog.Logger, db *database.DB) slog.Logger
is trying to do.
Maybe something like func (l Logger) AppendSinks(sinks ...Sink) Logger
would be useful.
Ah, I see what you mean.
Yea unfortunately, you have to use Make
to combine all the loggers at the top level before you start using Named
.
Can you do that in this case? If so, I'm thinking we forgo AppendSinks
for now.