Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
19b04fe070 | |||
4cd55875c6 | |||
a7896cc728 | |||
ff991bf49c |
@@ -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),
|
||||
|
@@ -11,6 +11,34 @@ type (
|
||||
mdKey struct{}
|
||||
)
|
||||
|
||||
// MustIncomingContext returns metadata from incoming ctx
|
||||
// returned metadata shoud not be modified or race condition happens.
|
||||
// If metadata not exists panics.
|
||||
func MustIncomingContext(ctx context.Context) Metadata {
|
||||
if ctx == nil {
|
||||
panic("missing metadata")
|
||||
}
|
||||
md, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata)
|
||||
if !ok {
|
||||
panic("missing metadata")
|
||||
}
|
||||
return md.md
|
||||
}
|
||||
|
||||
// MustOutgoingContext returns metadata from outgoing ctx
|
||||
// returned metadata shoud not be modified or race condition happens.
|
||||
// If metadata not exists panics.
|
||||
func MustOutgoingContext(ctx context.Context) Metadata {
|
||||
if ctx == nil {
|
||||
panic("missing metadata")
|
||||
}
|
||||
md, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata)
|
||||
if !ok {
|
||||
panic("missing metadata")
|
||||
}
|
||||
return md.md
|
||||
}
|
||||
|
||||
// FromIncomingContext returns metadata from incoming ctx
|
||||
// returned metadata shoud not be modified or race condition happens
|
||||
func FromIncomingContext(ctx context.Context) (Metadata, bool) {
|
||||
|
@@ -67,6 +67,22 @@ func (md Metadata) Iterator() *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
func (md Metadata) MustGet(key string) string {
|
||||
// fast path
|
||||
val, ok := md[key]
|
||||
if !ok {
|
||||
// slow path
|
||||
val, ok = md[textproto.CanonicalMIMEHeaderKey(key)]
|
||||
if !ok {
|
||||
val, ok = md[strings.ToLower(key)]
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
panic("missing metadata key")
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Get returns value from metadata by key
|
||||
func (md Metadata) Get(key string) (string, bool) {
|
||||
// fast path
|
||||
|
Reference in New Issue
Block a user