Merge pull request 'logger/slog: fix dedup keys' (#374) from loggerfix into v3
Reviewed-on: #374
This commit is contained in:
commit
0e546c5f87
@ -106,7 +106,9 @@ func WithAddFields(fields ...interface{}) Option {
|
||||
}
|
||||
}
|
||||
}
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
if len(fields) > 0 {
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
}
|
||||
} else {
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
}
|
||||
|
@ -142,6 +142,7 @@ func (s *slogLogger) Fields(fields ...interface{}) logger.Logger {
|
||||
s.mu.RUnlock()
|
||||
|
||||
l := &slogLogger{opts: options}
|
||||
logger.WithAddFields(fields...)(&l.opts)
|
||||
|
||||
if len(options.ContextAttrFuncs) == 0 {
|
||||
options.ContextAttrFuncs = logger.DefaultContextAttrFuncs
|
||||
|
@ -15,7 +15,29 @@ import (
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
func TestWithDuplicate(t *testing.T) {
|
||||
func TestWithFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithDedupKeys(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg1")
|
||||
|
||||
l = l.Fields("key1", "val2")
|
||||
|
||||
l.Info(ctx, "msg2")
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`msg=msg2 key1=val1`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDedupKeysWithAddFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
|
||||
|
Loading…
x
Reference in New Issue
Block a user