mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
log/slog: make TextHandler discard empty Source
Fixes #73808 Change-Id: Ica4b7a63eebbf0fff41d68f4de928f9da90c8ada Reviewed-on: https://go-review.googlesource.com/c/go/+/674875 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
c07ffe980a
commit
3fd729b2a1
3 changed files with 7 additions and 1 deletions
|
|
@ -492,6 +492,9 @@ func (s *handleState) appendAttr(a Attr) bool {
|
|||
// Special case: Source.
|
||||
if v := a.Value; v.Kind() == KindAny {
|
||||
if src, ok := v.Any().(*Source); ok {
|
||||
if src.isEmpty() {
|
||||
return false
|
||||
}
|
||||
if s.h.json {
|
||||
a.Value = src.group()
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -652,7 +652,7 @@ func TestJSONAndTextHandlersWithUnavailableSource(t *testing.T) {
|
|||
h Handler
|
||||
want string
|
||||
}{
|
||||
{"text", NewTextHandler(&buf, opts), "source=:0 msg=message"},
|
||||
{"text", NewTextHandler(&buf, opts), "msg=message"},
|
||||
{"json", NewJSONHandler(&buf, opts), `{"msg":"message"}`},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -211,6 +211,9 @@ func (s *Source) group() Value {
|
|||
return GroupValue(as...)
|
||||
}
|
||||
|
||||
// isEmpty returns whether the Source struct is nil or only contains zero fields.
|
||||
func (s *Source) isEmpty() bool { return s == nil || *s == Source{} }
|
||||
|
||||
// Source returns a new Source for the log event using r's PC.
|
||||
// If the PC field is zero, meaning the Record was created without the necessary information
|
||||
// or the location is unavailable, then nil is returned.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue