Skip to content

Commit

Permalink
op-service/testlog: reverse Attrs traversal order
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Aug 14, 2024
1 parent 40d8805 commit 00e33a9
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions op-service/testlog/capturing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@ type CapturedAttributes struct {
// Attrs calls f on each Attr in the [CapturedAttributes].
// Iteration stops if f returns false.
func (r *CapturedAttributes) Attrs(f func(slog.Attr) bool) {
searching := true
if r.Parent != nil {
r.Parent.Attrs(func(a slog.Attr) bool {
searching = f(a)
return searching
})
}
if !searching { // if the parent attributes context traversal found it already, then don't traverse the remainder
return
}
for _, a := range r.Attributes {
if !f(a) {
return
}
}
if r.Parent != nil {
r.Parent.Attrs(f)
}
}

// CapturedRecord is a wrapped around a regular log-record,
Expand All @@ -45,16 +38,16 @@ type CapturedRecord struct {
// Iteration stops if f returns false.
func (r *CapturedRecord) Attrs(f func(slog.Attr) bool) {
searching := true
if r.Parent != nil {
r.Parent.Attrs(func(a slog.Attr) bool {
searching = f(a)
return searching
})
}
if !searching { // if the parent attributes context traversal found it already, then don't traverse the remainder
r.Record.Attrs(func(a slog.Attr) bool {
searching = f(a)
return searching
})
if !searching { // if we found it already, then don't traverse the remainder
return
}
r.Record.Attrs(f)
if r.Parent != nil {
r.Parent.Attrs(f)
}
}

// CapturingHandler provides a log handler that captures all log records and optionally forwards them to a delegate.
Expand Down

0 comments on commit 00e33a9

Please sign in to comment.