Debian Codegraph @

handlers/json/json_test.go

raw
1 package json_test
2
3 import (
4 "bytes"
5 "testing"
6
7 qt "github.com/frankban/quicktest"
8
9 "github.com/bep/clocks"
10 "github.com/bep/logg"
11 "github.com/bep/logg/handlers/json"
12 )
13
14 func TestJSONHandler(t *testing.T) {
15 var buf bytes.Buffer
16
17 l := logg.New(
18 logg.Options{
19 Level: logg.LevelInfo,
20 Handler: json.New(&buf),
21 Clock: clocks.Fixed(clocks.TimeCupFinalNorway1976),
22 })
23
24 info := l.WithLevel(logg.LevelInfo)
25
26 info.WithField("user", "tj").WithField("id", "123").Log(logg.String("hello"))
27 info.Log(logg.String("world"))
28 info.WithLevel(logg.LevelError).Log(logg.String("boom"))
29
30 expected := "{\"level\":\"info\",\"timestamp\":\"1976-10-24T12:15:02.127686412Z\",\"fields\":[{\"name\":\"user\",\"value\":\"tj\"},{\"name\":\"id\",\"value\":\"123\"}],\"message\":\"hello\"}\n{\"level\":\"info\",\"timestamp\":\"1976-10-24T12:15:02.127686412Z\",\"message\":\"world\"}\n{\"level\":\"error\",\"timestamp\":\"1976-10-24T12:15:02.127686412Z\",\"message\":\"boom\"}\n"
31
32 qt.Assert(t, buf.String(), qt.Equals, expected)
33 }