Skip to content

Commit

Permalink
Merge pull request #6 from tjhop/fix/no-nesting-empty-group-keys
Browse files Browse the repository at this point in the history
fix: don't nest empty group keys, better follow slog.Hanlder interface
  • Loading branch information
tjhop authored Jan 15, 2025
2 parents 81c3a7a + 11db26d commit 6a4f9f3
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,25 @@ func appendPair(pairs []any, groupPrefix string, attr slog.Attr) []any {

switch attr.Value.Kind() {
case slog.KindGroup:
if attr.Key != "" {
groupPrefix = groupPrefix + "." + attr.Key
}
for _, a := range attr.Value.Group() {
pairs = appendPair(pairs, groupPrefix, a)
attrs := attr.Value.Group()
if len(attrs) > 0 {
// Only process groups that have non-empty attributes
// to properly conform to slog.Handler interface
// contract.

if attr.Key != "" {
// If a group's key is empty, attributes should
// be inlined to properly conform to
// slog.Handler interface contract.
if groupPrefix != "" {
groupPrefix = groupPrefix + "." + attr.Key
} else {
groupPrefix = attr.Key
}
}
for _, a := range attrs {
pairs = appendPair(pairs, groupPrefix, a)
}
}
default:
key := attr.Key
Expand Down

0 comments on commit 6a4f9f3

Please sign in to comment.