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