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

proposal: slog: support instance to context value #66168

Closed
lingdor opened this issue Mar 7, 2024 · 4 comments
Closed

proposal: slog: support instance to context value #66168

lingdor opened this issue Mar 7, 2024 · 4 comments
Labels
Milestone

Comments

@lingdor
Copy link

lingdor commented Mar 7, 2024

Proposal Details

I have some package in my project like: gorm , gin, slog. I don't want dependant on gorm and gin, How can i use traceid linked log content between gorm and gin module?

I want:

package gin

func Request1(){

logger :=slog.With("traceid",123)
ctx:=context.Background()
ctx=slog.NewContext(ctx,logger)
gorm.ReadDB(ctx)
}
package  gorm

func ReadDB(ctx context.Context){
     logger:=slog.DefaultContext(ctx)
    logger.Info("db is ready")
}

so, all modules can write one attribute in one context , and don't need rewrite slog handle.

my code is: golang/exp@master...lingdor:exp:master

thanks

@ianlancetaylor
Copy link
Contributor

CC @jba

@jba
Copy link
Contributor

jba commented Mar 7, 2024

We considered a function like slog.NewContext when building slog, and actually had it for a while, but it proved too controversial to include (see the last paragraph of that comment). You can easily write your own functions to put a logger in a context and retrieve it; see the documentation for context.Context.Value.

@seankhliao seankhliao changed the title proposal: exp/slog: support instance to context value proposal: slog: support instance to context value Mar 7, 2024
@seankhliao
Copy link
Member

Duplicate of #62194

@seankhliao seankhliao marked this as a duplicate of #62194 Mar 7, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 7, 2024
@veqryn
Copy link
Contributor

veqryn commented Apr 9, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants