-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
55 lines (44 loc) · 998 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package errands
import (
"context"
log "github.com/sirupsen/logrus"
)
const (
ContextLogger = "logger"
ContextRequestID = "request-id"
)
// Context implements api_context.Context for Errands.
type Context struct {
context.Context
ID string
logger *log.Entry
}
func NewContext(parentCtx context.Context, errandID string) *Context {
return &Context{
Context: parentCtx,
ID: errandID,
logger: log.WithField("errand_id", errandID),
}
}
func (c *Context) RequestID() string {
return c.ID
}
func (c *Context) Logger() *log.Entry {
return c.logger
}
func (c *Context) AddFieldsToLogger(fields log.Fields) {
c.logger = c.logger.WithFields(fields)
}
func (c *Context) AddFieldToLogger(key string, value interface{}) {
c.logger = c.logger.WithField(key, value)
}
func (c *Context) Value(key interface{}) interface{} {
switch key {
case ContextLogger:
return c.Logger()
case ContextRequestID:
return c.RequestID()
default:
return c.Context.Value(key)
}
}