-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: log/slog: invoke LogValue inside slog.Value.String #62636
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
Comments
cc @jba |
A related question is whether Example (playground: package main
import (
"fmt"
"log/slog"
"os"
)
var _ slog.LogValuer = coordinatesValuer{}
type coordinatesValuer struct {
x, y int
}
func (c coordinatesValuer) LogValue() slog.Value {
return slog.GroupValue(slog.Attr{Key: "X", Value: slog.IntValue(c.x)}, slog.Attr{Key: "Y", Value: slog.IntValue(c.y)})
}
func main() {
jsonHandler := slog.NewJSONHandler(os.Stdout, nil)
textHandler := slog.NewTextHandler(os.Stdout, nil)
coordinates := coordinatesValuer{1, 2}
coordinatesAttr := slog.Any("coordinates", coordinates)
coordinatesGroup := slog.Group("group", coordinatesAttr)
slog.New(jsonHandler).Info("JSON", "value", coordinates, coordinatesGroup)
slog.New(textHandler).Info("Text", "value", coordinates, coordinatesGroup)
fmt.Println(coordinatesGroup)
} Output:
|
I can think of two cases to call
So I lean towards "no." Are there other cases I haven't thought of? Same answer for |
Anyway, unless we think this is a real bug, we can't change it. |
Correct.
While debugging, I want useful output. Anyway, this is subjective. I'm okay with leaving it as-is. |
slog.Value
implementsfmt.Stringer
. The result ofString
depends on the kind of the value (int, group, etc.). When the kind is "any" and the type implementsslog.LogValuer
, that type'sLogValue
method is currently not called.I'm a bit on the edge whether it should be called, with a slight tendency towards "yes". My understanding is that
String
is meant to return a pretty-printed version of the value for users or developers. Same withLogValue
. Therefore usingLogValue
inString
makes sense to me.Example (playground:
Current output:
Expected (?) output:
The text was updated successfully, but these errors were encountered: