You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package nlog
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var (
|
|
fieldsContextKey contextKey
|
|
globalFields atomic.Value
|
|
globalFieldsLock sync.Mutex
|
|
)
|
|
|
|
type contextKey struct{}
|
|
|
|
// AddGlobalFields adds global fields.
|
|
func AddGlobalFields(fields ...LogField) {
|
|
globalFieldsLock.Lock()
|
|
defer globalFieldsLock.Unlock()
|
|
|
|
old := globalFields.Load()
|
|
if old == nil {
|
|
globalFields.Store(append([]LogField(nil), fields...))
|
|
} else {
|
|
globalFields.Store(append(old.([]LogField), fields...))
|
|
}
|
|
}
|
|
|
|
// ContextWithFields returns a new context with the given fields.
|
|
func ContextWithFields(ctx context.Context, fields ...LogField) context.Context {
|
|
if val := ctx.Value(fieldsContextKey); val != nil {
|
|
if arr, ok := val.([]LogField); ok {
|
|
allFields := make([]LogField, 0, len(arr)+len(fields))
|
|
allFields = append(allFields, arr...)
|
|
allFields = append(allFields, fields...)
|
|
return context.WithValue(ctx, fieldsContextKey, allFields)
|
|
}
|
|
}
|
|
|
|
return context.WithValue(ctx, fieldsContextKey, fields)
|
|
}
|
|
|
|
// WithFields returns a new logger with the given fields.
|
|
// deprecated: use ContextWithFields instead.
|
|
func WithFields(ctx context.Context, fields ...LogField) context.Context {
|
|
return ContextWithFields(ctx, fields...)
|
|
}
|