-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextHook_test.go
103 lines (72 loc) · 4.08 KB
/
ContextHook_test.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package logrus_context_hook
import (
"testing"
"context"
"github.com/sirupsen/logrus"
. "gopkg.in/check.v1"
)
/**********************************************************************************/
func TestFieldTransformationHookBackend(t *testing.T) { TestingT(t) }
type BackendFieldTransformationHookSuite struct {
}
var _ = Suite(&BackendFieldTransformationHookSuite{})
func (s *BackendFieldTransformationHookSuite) TestEasiestCase(c *C) {
// New Logger so we don't contaminate global state
log := logrus.New()
// Add the Context Hook
log.AddHook(NewContextHook("*", // * is special for ANY field of type context.Context.
[]string{"ServerId", "RequestId", "HostId"})) //Second parameter is the list of keys in that context to look for.
// Then log with as you do
log.Infof("Test1: Hello from Logger...")
// OR Log with Context field name
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test1: Hello from Logger with context under field 'Context'")
// OR Log with Context under any field name (because of the "*")
ctx = context.WithValue(context.Background(), "RequestId", "Want this logged anyway")
log.WithField("NotContextField", ctx).Infof("Test1: Hello from Logger with context under field 'NotContextField'")
}
func (s *BackendFieldTransformationHookSuite) TestSpecificContextField(c *C) {
// New Logger so we don't contaminate global state
log := logrus.New()
// Add the Context Hook
log.AddHook(NewContextHook("Context", // First parameter is the field name to look for context at
[]string{"ServerId", "RequestId", "HostId"})) //Second parameter is the list of keys in that context to look for.
// Then log with as you do
log.Infof("Test2: Hello from Logger...")
// OR Log with Context field Name
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test2: Hello from Logger with context under field 'Context'")
// EXCEPT Log without Context field name, and hook does nothing
ctx = context.WithValue(context.Background(), "RequestId", "Don't Want this logged")
log.WithField("NotContextField", ctx).Infof("Test2: Hello from Logger with context under field 'NotContextField'")
}
func (s *BackendFieldTransformationHookSuite) TestHookModification(c *C) {
// New Logger so we don't contaminate global state
log := logrus.New()
// Hold reference to the hook
ctxHook := NewContextHook("Context", // First parameter is the field to look for context at
[]string{"ServerId", "RequestId", "HostId"}) //Second parameter is the list of keys in that context to look for.
// Add the Context Hook
log.AddHook(ctxHook)
// Then log with as you do
log.Infof("Test3: Hello from Logger...")
// OR Log with Context field
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test3: Hello from Logger with context under field 'Context'")
// EXCEPT Log without Context field, and hook does nothing
ctx = context.WithValue(context.Background(), "RequestId", "Don't Want this logged")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context under field 'NotContextField'")
// BUT we can change that at runtime
ctxHook.SetContextField("*")
// AND it works now...
ctx = context.WithValue(context.Background(), "RequestId", "Changed our mind at runtime. We want this logged.")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context under field 'NotContextField'")
// EXCEPT we just need a new key called UserId
ctx = context.WithValue(context.Background(), "UserId", "This key wasn't being looked for")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context having new key 'UserId'")
// BUT we can change that at runtime TOO
ctxHook.SetContextKeys(append(ctxHook.GetContextKeys(), "UserId"))
// AMD it works now
ctx = context.WithValue(context.Background(), "UserId", "This key is being looked for. Changed our mind at runtime again.")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context having new key 'UserId'")
}