feat: support logx.WithFields (#2128)

This commit is contained in:
Kevin Wan
2022-07-11 23:19:26 +08:00
committed by GitHub
parent 6e50c87dca
commit 24787a946b
5 changed files with 103 additions and 21 deletions

18
core/logx/fields.go Normal file
View File

@@ -0,0 +1,18 @@
package logx
import "context"
var fieldsContextKey contextKey
type contextKey struct{}
// WithFields returns a new context with the given fields.
func WithFields(ctx context.Context, fields ...LogField) context.Context {
if val := ctx.Value(fieldsContextKey); val != nil {
if arr, ok := val.([]LogField); ok {
return context.WithValue(ctx, fieldsContextKey, append(arr, fields...))
}
}
return context.WithValue(ctx, fieldsContextKey, fields)
}