-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add labels inside and outside group. #1
Comments
I don't think it's possible to accomplish that, since when the log is actually happening (the I've played around a bit with specifying group paths for the context values: var alwaysLog ctxkey.Key[string]
var logInGroup ctxkey.Key[string]
var logInSubGroup ctxkey.Key[string]
func main() {
var logHandler slog.Handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
})
logHandler = alwaysLog.AttachToLog("attachAlways", logHandler)
logHandler = logInGroup.AttachToLogGroup([]string{"group"}, "attachToGroup", logHandler)
logHandler = logInSubGroup.AttachToLogGroup([]string{"group", "subGroup"}, "attachToSubGroup", logHandler)
slog.SetDefault(slog.New(logHandler))
logger := slog.Default()
ctx := context.Background()
ctx = alwaysLog.WithValue(ctx, "ok")
ctx = logInGroup.WithValue(ctx, "ok")
ctx = logInSubGroup.WithValue(ctx, "ok")
logger.InfoContext(ctx, "0 layers")
subLogger := logger.WithGroup("group")
subLogger.InfoContext(ctx, "1 layer")
subSubLogger := subLogger.WithGroup("subGroup")
subSubLogger.InfoContext(ctx, "2 layers")
} output: {"time":"2024-07-17T19:28:44.806435125+09:00","level":"INFO","msg":"0 layers","attachAlways":"ok"}
{"time":"2024-07-17T19:28:44.806574133+09:00","level":"INFO","msg":"1 layer","group":{"attachToGroup":"ok","attachAlways":"ok"}}
{"time":"2024-07-17T19:28:44.806580305+09:00","level":"INFO","msg":"2 layers","group":{"subGroup":{"attachToSubGroup":"ok","attachToGroup":"ok","attachAlways":"ok"}}} Notice how |
Hello, thank you very much, in the end we solved it in a more traditional way. I have generated a handler to manage my context
On the other hand, I configure my logger in the following way ...
I can't find the methods you indicate in your message.
This generates the following result
Sorry for the possible errors but I'm new to golang |
I see, yea, that's probably the cleanest way to achieve what you want. func getAttr(ctx context.Context, val ctxkey.Key[string]) string {
v, err := val.Value(ctx)
if err != nil {
return ""
}
return v
} You could probably even strip it down to something like this, to make it more inlinable, maybe even make it generic. func getAttr(ctx context.Context, val ctxkey.Key[string]) (v string) {
v, _ = val.Value(ctx)
return
}
|
Hi Get Attr have the next code.
The bad thing about this implementation is having to reimplement all the logger methods (debug, error, info, ....) |
Hello, what happens if I wanted to add labels both inside and outside a group, for example
keeping in mind that both the values shown in properties.verb and X-B3-SpanId are values stored in the context
The text was updated successfully, but these errors were encountered: