9 "github.com/go-kit/kit/log"
12 func TestJSONLoggerCaller(t *testing.T) {
14 buf := &bytes.Buffer{}
15 logger := log.NewJSONLogger(buf)
16 logger = log.With(logger, "caller", log.DefaultCaller)
18 if err := logger.Log(); err != nil {
21 if want, have := `{"caller":"json_logger_test.go:18"}`+"\n", buf.String(); want != have {
22 t.Errorf("\nwant %#v\nhave %#v", want, have)
26 func TestJSONLogger(t *testing.T) {
28 buf := &bytes.Buffer{}
29 logger := log.NewJSONLogger(buf)
30 if err := logger.Log("err", errors.New("err"), "m", map[string]int{"0": 0}, "a", []int{1, 2, 3}); err != nil {
33 if want, have := `{"a":[1,2,3],"err":"err","m":{"0":0}}`+"\n", buf.String(); want != have {
34 t.Errorf("\nwant %#v\nhave %#v", want, have)
38 func TestJSONLoggerMissingValue(t *testing.T) {
40 buf := &bytes.Buffer{}
41 logger := log.NewJSONLogger(buf)
42 if err := logger.Log("k"); err != nil {
45 if want, have := `{"k":"(MISSING)"}`+"\n", buf.String(); want != have {
46 t.Errorf("\nwant %#v\nhave %#v", want, have)
50 func TestJSONLoggerNilStringerKey(t *testing.T) {
53 buf := &bytes.Buffer{}
54 logger := log.NewJSONLogger(buf)
55 if err := logger.Log((*stringer)(nil), "v"); err != nil {
58 if want, have := `{"NULL":"v"}`+"\n", buf.String(); want != have {
59 t.Errorf("\nwant %#v\nhave %#v", want, have)
63 func TestJSONLoggerNilErrorValue(t *testing.T) {
66 buf := &bytes.Buffer{}
67 logger := log.NewJSONLogger(buf)
68 if err := logger.Log("err", (*stringError)(nil)); err != nil {
71 if want, have := `{"err":null}`+"\n", buf.String(); want != have {
72 t.Errorf("\nwant %#v\nhave %#v", want, have)
76 // aller implements json.Marshaler, encoding.TextMarshaler, and fmt.Stringer.
79 func (aller) MarshalJSON() ([]byte, error) {
80 return []byte("\"json\""), nil
83 func (aller) MarshalText() ([]byte, error) {
84 return []byte("text"), nil
87 func (aller) String() string {
91 func (aller) Error() string {
95 // textstringer implements encoding.TextMarshaler and fmt.Stringer.
96 type textstringer struct{}
98 func (textstringer) MarshalText() ([]byte, error) {
99 return []byte("text"), nil
102 func (textstringer) String() string {
106 func TestJSONLoggerStringValue(t *testing.T) {
114 expected: `{"v":"json"}`,
118 expected: `{"v":"text"}`,
121 v: stringer("string"),
122 expected: `{"v":"string"}`,
126 for _, test := range tests {
127 buf := &bytes.Buffer{}
128 logger := log.NewJSONLogger(buf)
129 if err := logger.Log("v", test.v); err != nil {
133 if want, have := test.expected+"\n", buf.String(); want != have {
134 t.Errorf("\nwant %#v\nhave %#v", want, have)
141 func (s stringer) String() string {
145 type stringError string
147 func (s stringError) Error() string {
151 func BenchmarkJSONLoggerSimple(b *testing.B) {
152 benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard), baseMessage)
155 func BenchmarkJSONLoggerContextual(b *testing.B) {
156 benchmarkRunner(b, log.NewJSONLogger(ioutil.Discard), withMessage)
159 func TestJSONLoggerConcurrency(t *testing.T) {
161 testConcurrency(t, log.NewJSONLogger(ioutil.Discard), 10000)