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

WithValue on contex with existing fields affects the parent context #5

Closed
deansg opened this issue May 21, 2024 · 2 comments · Fixed by #6
Closed

WithValue on contex with existing fields affects the parent context #5

deansg opened this issue May 21, 2024 · 2 comments · Fixed by #6

Comments

@deansg
Copy link
Contributor

deansg commented May 21, 2024

When calling WithValue on a ctx.Context that already has some attached fields, it will affect the parent context as well. For example:

package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/PumpkinSeed/slog-context"
)

func main() {
	slog.SetDefault(slog.New(slogcontext.NewHandler(slog.NewJSONHandler(os.Stdout, nil))))

	ctx := slogcontext.WithValue(context.Background(), "number", 12)
        ctx2 := slogcontext.WithValue(ctx, "stringField", "data")
	slog.InfoContext(ctx, "log1") // This will contain the stringField field even though it was meant to be added only to ctx2
	slog.InfoContext(ctx2, "log2")
}
@deansg
Copy link
Contributor Author

deansg commented May 28, 2024

@PumpkinSeed hi, it seems like you're the sole maintainer in this repo, so your review here would be much appreciated

@PumpkinSeed
Copy link
Owner

@deansg Thanks for your PR, I reviewed it and looks good. I released v0.1.2 which has this fix.

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