-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_target_test.go
99 lines (72 loc) · 2.38 KB
/
json_target_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
package blackbox_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/RobertWHurst/blackbox"
"github.com/stretchr/testify/assert"
)
type JSONOutput struct {
Context blackbox.Ctx
Level string
Message string
Time string
}
func TestJsonTarget(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
jsonTarget := blackbox.NewJSONTarget(outBuf, errBuf)
values := make([]interface{}, 1)
values[0] = "Hello Test"
jsonTarget.Log(blackbox.Trace, values, blackbox.Ctx{"key": "value"})
var output JSONOutput
json.Unmarshal(outBuf.Bytes(), &output)
assert.NotEmpty(t, output.Time)
assert.Equal(t, "Hello Test", output.Message)
assert.Equal(t, "trace", output.Level)
assert.Equal(t, "value", output.Context["key"])
}
func TestJsonTargetSetLevel(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
jsonTarget := blackbox.NewJSONTarget(outBuf, errBuf)
jsonTarget.SetLevel(blackbox.Info)
values := make([]interface{}, 1)
values[0] = "Filtered Message"
jsonTarget.Log(blackbox.Trace, values, blackbox.Ctx{"x": "y"})
values = make([]interface{}, 1)
values[0] = "Hello Test"
jsonTarget.Log(blackbox.Info, values, blackbox.Ctx{"key": "value"})
assert.NotRegexp(t, `Filtered Message`, outBuf.String())
assert.Regexp(t, `Hello Test`, outBuf.String())
}
func TestJsonTargetShowTimestamp(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
jsonTarget := blackbox.NewJSONTarget(outBuf, errBuf)
jsonTarget.ShowTimestamp(false)
values := make([]interface{}, 1)
values[0] = "Hello Test"
jsonTarget.Log(blackbox.Trace, values, blackbox.Ctx{"key": "value"})
var output JSONOutput
json.Unmarshal(outBuf.Bytes(), &output)
assert.Empty(t, output.Time)
assert.Equal(t, "Hello Test", output.Message)
assert.Equal(t, "trace", output.Level)
assert.Equal(t, "value", output.Context["key"])
}
func TestJsonTargetShowContext(t *testing.T) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
jsonTarget := blackbox.NewJSONTarget(outBuf, errBuf)
jsonTarget.ShowContext(false)
values := make([]interface{}, 1)
values[0] = "Hello Test"
jsonTarget.Log(blackbox.Trace, values, blackbox.Ctx{"key": "value"})
var output JSONOutput
json.Unmarshal(outBuf.Bytes(), &output)
assert.NotEmpty(t, output.Time)
assert.Equal(t, "Hello Test", output.Message)
assert.Equal(t, "trace", output.Level)
assert.Empty(t, output.Context)
}