logger: add WithAddFields option
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
89cf4ef8af
commit
115ca6a018
@ -80,6 +80,13 @@ func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithAddFields add fields for the logger
|
||||
func WithAddFields(fields ...interface{}) Option {
|
||||
return func(o *Options) {
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
// WithFields set default fields for the logger
|
||||
func WithFields(fields ...interface{}) Option {
|
||||
return func(o *Options) {
|
||||
|
@ -15,6 +15,34 @@ import (
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
)
|
||||
|
||||
func TestWithAddFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf))
|
||||
if err := l.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg1")
|
||||
|
||||
if err := l.Init(logger.WithAddFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l.Info(ctx, "msg2")
|
||||
|
||||
if err := l.Init(logger.WithAddFields("key2", "val2")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l.Info(ctx, "msg3")
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`"key1"`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`"key2"`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleFieldsWithLevel(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
Loading…
Reference in New Issue
Block a user