feat: support logx.WithFields (#2128)
This commit is contained in:
35
core/logx/fields_test.go
Normal file
35
core/logx/fields_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithFields(t *testing.T) {
|
||||
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||
}
|
||||
|
||||
func TestWithFieldsAppend(t *testing.T) {
|
||||
var dummyKey struct{}
|
||||
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
||||
ctx = WithFields(ctx, Field("a", 1), Field("b", 2))
|
||||
ctx = WithFields(ctx, Field("c", 3), Field("d", 4))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "dummy", ctx.Value(dummyKey))
|
||||
assert.EqualValues(t, []LogField{
|
||||
Field("a", 1),
|
||||
Field("b", 2),
|
||||
Field("c", 3),
|
||||
Field("d", 4),
|
||||
}, fields)
|
||||
}
|
||||
Reference in New Issue
Block a user