github.com/Axway/agent-sdk@v1.1.101/pkg/util/log/context.go (about) 1 package log 2 3 import ( 4 "context" 5 6 "github.com/sirupsen/logrus" 7 ) 8 9 type ContextField string 10 11 const ( 12 KindCtx ContextField = "kind" 13 NameCtx ContextField = "name" 14 ) 15 16 var allCtxFields = map[ContextField]struct{}{KindCtx: {}, NameCtx: {}} 17 18 func RegisterContextField(ctxFields ...ContextField) { 19 for _, ctxField := range ctxFields { 20 allCtxFields[ctxField] = struct{}{} 21 } 22 } 23 24 // NewLoggerFromContext returns a FieldLogger for standard logging, and logp logging. 25 func NewLoggerFromContext(ctx context.Context) FieldLogger { 26 entry := logrus.NewEntry(log) 27 logger := &logger{ 28 entry: entry, 29 } 30 return UpdateLoggerWithContext(ctx, logger) 31 } 32 33 // UpdateLoggerWithContext returns a FieldLogger for standard logging, and logp logging. 34 func UpdateLoggerWithContext(ctx context.Context, logger FieldLogger) FieldLogger { 35 for field := range allCtxFields { 36 if v := ctx.Value(field); v != nil { 37 logger.WithField(string(field), v) 38 } 39 } 40 return logger 41 }