You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The encoding/json package marshals a slog.Value as {}. That means if a slog.Value appears inside something that is logged, slog.JSONHandler (and any other handler that outputs JSON and doesn't treat slog.Value specially) will lose its information. (See #62699.)
To fix that, I propose adding
package slog
func (v Value) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Any())
}
The text was updated successfully, but these errors were encountered:
package main
import (
"log/slog""os"
)
// A token is a secret value that grants permissions.typeTokenstring// LogValue implements slog.LogValuer.// It avoids revealing the token.func (Token) LogValue() slog.Value {
returnslog.StringValue("REDACTED_TOKEN")
}
// This example demonstrates a Value that replaces itself// with an alternative representation to avoid revealing secrets.funcmain() {
t:=Token("shhhh!")
logger:=slog.New(slog.NewTextHandler(os.Stdout, nil))
logger.Info("permission granted", "user", "Perry", "token", t)
tokens:= []Token{"keep it secret", "keep it safe"}
logger.Info("permission revoked", "tokens", tokens)
}
I would like to see the secrets staying secret, but the design today does not respect slog.LogValuer in those contexts, leading to unfortunate consequences (a less disastrous version of which I have run into in real application code).
Because of that behavior, I've found that for custom types that need to reliably serialize, implementing slog.LogValuer is not actually sufficient—you also need to implement MarshalJSON. Which in some cases is unfortunate, and in others where there is already a canonical JSON representation that is for whatever reason undesirable for logging, a separate type or manual serialization is required.
I'm not opposed to this proposal, but I'm wondering if there isn't a solution that solves the problem more generally. The ideal behavior IMO would be that for slog-serialized JSON, LogValue() takes precedence over MarshalJSON() at any level of nesting in slices, structs, etc. I'm not sure if there's a simple way to make something like that work.
The
encoding/json
package marshals aslog.Value
as{}
. That means if aslog.Value
appears inside something that is logged,slog.JSONHandler
(and any other handler that outputs JSON and doesn't treatslog.Value
specially) will lose its information. (See #62699.)To fix that, I propose adding
The text was updated successfully, but these errors were encountered: