fix: handler should inline group with empty key#33
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #33 +/- ##
==========================================
+ Coverage 52.98% 54.33% +1.34%
==========================================
Files 4 4
Lines 251 254 +3
==========================================
+ Hits 133 138 +5
+ Misses 115 113 -2
Partials 3 3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I agree Let me update the 30+ pkg 😅 |
|
@samber thanks for maintaining all of these There is 1 more issue with log attributes that implement |
|
Thanks @AVM-Martin for both fixes. Did you find more bugs ? Can I upgrade every |
|
For now, I don't see another bugs. package main_test
import (
"bytes"
"encoding/json"
"log/slog"
"testing"
"testing/slogtest"
"github.com/rs/zerolog"
slogzerolog "github.com/samber/slog-zerolog/v2"
)
func TestNewHandler(t *testing.T) {
var buf bytes.Buffer
newHandler := func(t *testing.T) slog.Handler {
logger := zerolog.New(&buf)
return slogzerolog.Option{
Logger: &logger,
// Level: slog.LevelInfo,
}.NewZerologHandler()
}
result := func(t *testing.T) map[string]any {
defer buf.Reset()
var m map[string]any
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
t.Fatal(err)
}
if val, ok := m["message"]; ok {
// zerolog use "message" as the key, key replacement is needed
m[slog.MessageKey] = val
delete(m, "message")
}
if val, ok := m["time"]; ok && val == "0001-01-01T00:00:00Z" {
// zerolog does not ignore zero time, manual removal is needed
delete(m, "time")
}
return m
}
slogtest.Run(t, newHandler, result)
} |
It is true that we need to remove attributes with empty key, but we need to flatten nonempty groups with empty key
Refs: https://cs.opensource.google/go/go/+/refs/tags/go1.25.6:src/testing/slogtest/slogtest.go;l=117-129