forked from adamhassel/loggers-mapper-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_test.go
108 lines (93 loc) · 3.03 KB
/
newrelic_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
104
105
106
107
108
package newrelic
import (
"bytes"
syslog "log"
"regexp"
"testing"
"github.com/Vivino/go-loggers/mappers/stdlib"
newrelic "github.com/newrelic/go-agent"
)
func TestLoggersInterface(t *testing.T) {
var _ newrelic.Logger = NewDefaultLogger()
}
func TestLoggersLevelOutput(t *testing.T) {
l, b := newBufferedNRLog()
l.Info("This is a test", nil)
expectedMatch := "(?i)info.*This is a test"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestNRWithFieldsOutput(t *testing.T) {
l, b := newBufferedNRLog()
d := map[string]interface{}{
"test": true,
}
l.Warn("This is a message", d)
expectedMatch := "(?i)warn.*This is a message.*test.*=true"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestNRWithRedacting(t *testing.T) {
l, b := newBufferedNRLog()
d := map[string]interface{}{
"license_key": "123456abcd",
}
l.Warn("This is a message", d)
expectedMatch := `(?i)warn.*This is a message.*license_key=\[REDACTED\].*`
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestNRWithNoRedacting(t *testing.T) {
l, b := newBufferedNRLogUncensored()
d := map[string]interface{}{
"license_key": "123456abcd",
}
l.Warn("This is a message", d)
expectedMatch := `(?i)warn.*This is a message.*license_key=123456abcd.*`
actual := b.String()
if ok, _ := regexp.Match(expectedMatch, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch)
}
}
func TestNRWithMultipleFieldsOutput(t *testing.T) {
l, b := newBufferedNRLog()
d := map[string]interface{}{
"test": true,
"Error": "serious",
}
l.Error("This is a message", d)
// maps are random by design, so we can't know what order the fields will be in
expectedMatch1 := "(?i)erro.*This is a message"
expectedMatch2 := "test.*=true"
expectedMatch3 := "Error.*=serious"
actual := b.String()
if ok, _ := regexp.Match(expectedMatch1, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch1)
}
if ok, _ := regexp.Match(expectedMatch2, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch2)
}
if ok, _ := regexp.Match(expectedMatch3, []byte(actual)); !ok {
t.Errorf("Log output mismatch %s (actual) != %s (expected)", actual, expectedMatch3)
}
}
func newBufferedNRLog() (newrelic.Logger, *bytes.Buffer) {
var b []byte
var bb = bytes.NewBuffer(b)
sl := syslog.New(bb, "", 0)
l := stdlib.NewLogger(sl)
return NewLogger(l, true, true), bb
}
func newBufferedNRLogUncensored() (newrelic.Logger, *bytes.Buffer) {
var b []byte
var bb = bytes.NewBuffer(b)
sl := syslog.New(bb, "", 0)
l := stdlib.NewLogger(sl)
return NewLogger(l, true, false), bb
}