Skip to content

Make Named work with nested loggers #92

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

Closed
cmoog opened this issue Sep 2, 2020 · 4 comments
Closed

Make Named work with nested loggers #92

cmoog opened this issue Sep 2, 2020 · 4 comments

Comments

@cmoog
Copy link
Contributor

cmoog commented Sep 2, 2020

Similar to the issue with nested loggers not respecting Leveled, the same problem exists for Named.

@nhooyr
Copy link
Contributor

nhooyr commented Sep 2, 2020

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

@cmoog
Copy link
Contributor Author

cmoog commented Sep 2, 2020

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.

@cmoog
Copy link
Contributor Author

cmoog commented Sep 2, 2020

Maybe something like func (l Logger) AppendSinks(sinks ...Sink) Logger would be useful.

@nhooyr
Copy link
Contributor

nhooyr commented Sep 3, 2020

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.

@nhooyr nhooyr closed this as completed Sep 3, 2020
nhooyr added a commit that referenced this issue Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants