github.com/go-kit/log@v0.2.1/logfmt_logger_test.go (about) 1 package log_test 2 3 import ( 4 "bytes" 5 "errors" 6 "io/ioutil" 7 "testing" 8 9 "github.com/go-kit/log" 10 "github.com/go-logfmt/logfmt" 11 ) 12 13 func TestLogfmtLogger(t *testing.T) { 14 t.Parallel() 15 buf := &bytes.Buffer{} 16 logger := log.NewLogfmtLogger(buf) 17 18 if err := logger.Log("hello", "world"); err != nil { 19 t.Fatal(err) 20 } 21 if want, have := "hello=world\n", buf.String(); want != have { 22 t.Errorf("want %#v, have %#v", want, have) 23 } 24 25 buf.Reset() 26 if err := logger.Log("a", 1, "err", errors.New("error")); err != nil { 27 t.Fatal(err) 28 } 29 if want, have := "a=1 err=error\n", buf.String(); want != have { 30 t.Errorf("want %#v, have %#v", want, have) 31 } 32 33 buf.Reset() 34 if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil { 35 t.Fatal(err) 36 } 37 if want, have := "std_map=\""+logfmt.ErrUnsupportedValueType.Error()+"\" my_map=special_behavior\n", buf.String(); want != have { 38 t.Errorf("want %#v, have %#v", want, have) 39 } 40 } 41 42 func BenchmarkLogfmtLoggerSimple(b *testing.B) { 43 benchmarkRunner(b, log.NewLogfmtLogger(ioutil.Discard), baseMessage) 44 } 45 46 func BenchmarkLogfmtLoggerContextual(b *testing.B) { 47 benchmarkRunner(b, log.NewLogfmtLogger(ioutil.Discard), withMessage) 48 } 49 50 func TestLogfmtLoggerConcurrency(t *testing.T) { 51 t.Parallel() 52 testConcurrency(t, log.NewLogfmtLogger(ioutil.Discard), 10000) 53 } 54 55 type mymap map[int]int 56 57 func (m mymap) String() string { return "special_behavior" }