-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixes incorrect WithAttrs and WithGroup behaviour * Deduplicate attributes with the same key golang/go#59365
- Loading branch information
Showing
6 changed files
with
208 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package clog_test | ||
|
||
type Entry map[string]any | ||
|
||
func (e Entry) GetAny(key string) (any, bool) { | ||
v, ok := e[key] | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
return v, true | ||
} | ||
|
||
func (e Entry) GetString(key string) (string, bool) { | ||
v, ok := e.GetAny(key) | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
s, ok := v.(string) | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
return s, true | ||
} | ||
|
||
func (e Entry) GetMap(key string) (map[string]any, bool) { | ||
v, ok := e.GetAny(key) | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
m, ok := v.(map[string]any) | ||
if !ok { | ||
return nil, false | ||
} | ||
return m, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package clog | ||
|
||
import "log/slog" | ||
|
||
// Returns a new slog.Record with its attrs having the same key deduped. | ||
// Keys are case sensitive. | ||
func dedupAttrs(record slog.Record) slog.Record { | ||
attrMap := map[string]slog.Attr{} | ||
|
||
record.Attrs(func(a slog.Attr) bool { | ||
attrMap[a.Key] = a | ||
return true | ||
}) | ||
|
||
if len(attrMap) == 0 { | ||
return record | ||
} | ||
|
||
r := slog.NewRecord(record.Time, record.Level, record.Message, record.PC) | ||
|
||
for _, attr := range attrMap { | ||
// We could have used an extra array to call AddAttrs() once only, but we | ||
// want to avoid further allocs. | ||
r.AddAttrs(attr) | ||
} | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package clog_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ronny/clog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHandler_DuplicateAttrs(t *testing.T) { | ||
timestamp := time.Date(2024, time.May, 29, 12, 34, 56, 0, time.UTC) | ||
|
||
testCases := []struct { | ||
desc string | ||
ctx context.Context | ||
record func() slog.Record | ||
expected string | ||
}{ | ||
{ | ||
desc: "without attrs", | ||
ctx: context.Background(), | ||
record: func() slog.Record { | ||
return slog.NewRecord(timestamp, slog.LevelInfo, "hello", 0) | ||
}, | ||
expected: `{"time":"2024-05-29T12:34:56Z","severity":"INFO","message":"hello"}`, | ||
}, | ||
{ | ||
desc: "without duplicate attrs", | ||
ctx: context.Background(), | ||
record: func() slog.Record { | ||
r := slog.NewRecord(timestamp, slog.LevelInfo, "hello", 0) | ||
r.AddAttrs( | ||
slog.Attr{Key: "a", Value: slog.StringValue("one")}, | ||
) | ||
return r | ||
}, | ||
expected: `{"time":"2024-05-29T12:34:56Z","severity":"INFO","message":"hello","a":"one"}`, | ||
}, | ||
{ | ||
desc: "overwrites duplicate attrs", | ||
ctx: context.Background(), | ||
record: func() slog.Record { | ||
r := slog.NewRecord(timestamp, slog.LevelInfo, "hello", 0) | ||
r.AddAttrs( | ||
slog.Attr{Key: "a", Value: slog.StringValue("one")}, | ||
slog.Attr{Key: "a", Value: slog.StringValue("two")}, | ||
) | ||
return r | ||
}, | ||
expected: `{"time":"2024-05-29T12:34:56Z","severity":"INFO","message":"hello","a":"two"}`, | ||
}, | ||
} | ||
for i, tc := range testCases { | ||
tc := tc | ||
t.Run(fmt.Sprintf("%d: %s", i, tc.desc), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
buf := &bytes.Buffer{} | ||
|
||
handler, err := clog.NewHandler(buf, clog.HandlerOptions{ | ||
Level: clog.LevelInfo, | ||
GoogleProjectID: "my-project-id", | ||
}) | ||
if !assert.Nil(t, err) { | ||
return | ||
} | ||
|
||
err = handler.Handle(tc.ctx, tc.record()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, tc.expected, strings.TrimSpace(buf.String())) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters