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

Context's Logger() method returning a non-pointer #400

Closed
nickcorin opened this issue Jan 24, 2022 · 2 comments
Closed

Context's Logger() method returning a non-pointer #400

nickcorin opened this issue Jan 24, 2022 · 2 comments

Comments

@nickcorin
Copy link
Contributor

nickcorin commented Jan 24, 2022

As per the documentation for zerolog, using contexts to pass sub-loggers should work something like this:

ctx := log.With().Str("component", "module").Logger().WithContext(ctx)

log.Ctx(ctx).Info().Msg("hello world")

// Output: {"component":"module","level":"info","message":"hello world"}

This doesn't work, however, since the Logger() method on Context returns a non-pointer and WithContext(context.Context) requires a pointer receiver.

I have had to use a workaround that looks like this:

ctxLogger := log.With().Str("component", "module").Logger()
ctx := (&ctxLogger).WithContext(ctx)

log.Ctx(ctx).Info().Msg("hello world")

// Output: {"component":"module","level":"info","message":"hello world"}

It works, but is not pretty.

Were there any major design considerations for using a non-pointer receiver for Logger()?

I'm more than happy to create the PR to change this if you agree that it's a good idea.

@smyrman
Copy link

smyrman commented Feb 2, 2022

This issue is the same as #116, just with a different proposed solution.

@mitar
Copy link
Contributor

mitar commented Aug 18, 2023

This has been fixed with #409 and #499 by changing WithContext to not be pointer receiver and keeping Logger to be value receiver. I think this could be closed unless we want to discuss making all methods be of the same receiver type (also opened in #563).

@rs rs closed this as completed Aug 18, 2023
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

No branches or pull requests

4 participants