Skip to content
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

feat: modify slog.Logger to *not* satisfy slog.Sink #95

Merged
merged 6 commits into from
Mar 4, 2021

Conversation

cmoog
Copy link
Contributor

@cmoog cmoog commented Mar 4, 2021

The prior implementation allowed slog.Logger instances
to themselves be passes as slog.Sink targets. This enabled
many usage patterns that introduced extremely counter-intuitive
behavior.

This isn't just an aesthetics problem, but has caused real confusion in our monorepo. Consider the following case:

func prepareSomeService(log slog.Logger) *service {
  var srv service
  l = slog.Make(log, someOtherSink)
  srv.logger = l.Named("special-service")
  return &srv
}

Because of the nesting here, where log is a slog.Logger passed as a slog.Sink, the metadata between these two sink targets will be different. That can have unexpected behavior, like in this case logs to someOtherSink could have a different logger name than those logged to log.

As a solution, this PR modifes slog.Logger to not satisfy slog.Sink. This is a much simpler mental model... sinks are the primitive and loggers are higher-level that also hold metadata. Loggers also being sinks themselves creates more confusion than value.

To suite this particular use cases, whereby a child wants to extend a logger with an additional sinks, we should instead expose the following method

func (l Logger) AppendSinks(sinks ...Sink) Logger

The prior implementation allowed slog.Logger instances
to themselves be passes as slog.Sink targets. This enabled
many usage patterns that introduced extremely counter-intuitive
behavior.
Copy link
Member

@deansheather deansheather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine. I can't think of any use case for using a logger as a sink, it doesn't really make much sense.

slog.go Show resolved Hide resolved
example_helper_test.go Outdated Show resolved Hide resolved
@cmoog cmoog merged commit 552693c into master Mar 4, 2021
@cmoog cmoog deleted the cmoog/no-recursive-interface branch March 4, 2021 18:32
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 this pull request may close these issues.

None yet

3 participants