mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-25 02:08:44 +08:00
19 lines
459 B
Go
19 lines
459 B
Go
|
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)
|
||
|
}
|