github.com/DFWallet/tendermint-cosmos@v0.0.2/libs/log/tm_logger_test.go (about) 1 package log_test 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "strings" 7 "testing" 8 9 "github.com/DFWallet/tendermint-cosmos/libs/log" 10 ) 11 12 func TestLoggerLogsItsErrors(t *testing.T) { 13 var buf bytes.Buffer 14 15 logger := log.NewTMLogger(&buf) 16 logger.Info("foo", "baz baz", "bar") 17 msg := strings.TrimSpace(buf.String()) 18 if !strings.Contains(msg, "foo") { 19 t.Errorf("expected logger msg to contain ErrInvalidKey, got %s", msg) 20 } 21 } 22 23 func TestInfo(t *testing.T) { 24 var bufInfo bytes.Buffer 25 26 l := log.NewTMLogger(&bufInfo) 27 l.Info("Client initialized with old header (trusted is more recent)", 28 "old", 42, 29 "trustedHeight", "forty two", 30 "trustedHash", []byte("test me")) 31 32 msg := strings.TrimSpace(bufInfo.String()) 33 34 // Remove the timestamp information to allow 35 // us to test against the expected message. 36 receivedmsg := strings.Split(msg, "] ")[1] 37 38 const expectedmsg = `Client initialized with old header 39 (trusted is more recent) old=42 trustedHeight="forty two" 40 trustedHash=74657374206D65` 41 if strings.EqualFold(receivedmsg, expectedmsg) { 42 t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg) 43 } 44 } 45 46 func TestDebug(t *testing.T) { 47 var bufDebug bytes.Buffer 48 49 ld := log.NewTMLogger(&bufDebug) 50 ld.Debug("Client initialized with old header (trusted is more recent)", 51 "old", 42, 52 "trustedHeight", "forty two", 53 "trustedHash", []byte("test me")) 54 55 msg := strings.TrimSpace(bufDebug.String()) 56 57 // Remove the timestamp information to allow 58 // us to test against the expected message. 59 receivedmsg := strings.Split(msg, "] ")[1] 60 61 const expectedmsg = `Client initialized with old header 62 (trusted is more recent) old=42 trustedHeight="forty two" 63 trustedHash=74657374206D65` 64 if strings.EqualFold(receivedmsg, expectedmsg) { 65 t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg) 66 } 67 } 68 69 func TestError(t *testing.T) { 70 var bufErr bytes.Buffer 71 72 le := log.NewTMLogger(&bufErr) 73 le.Error("Client initialized with old header (trusted is more recent)", 74 "old", 42, 75 "trustedHeight", "forty two", 76 "trustedHash", []byte("test me")) 77 78 msg := strings.TrimSpace(bufErr.String()) 79 80 // Remove the timestamp information to allow 81 // us to test against the expected message. 82 receivedmsg := strings.Split(msg, "] ")[1] 83 84 const expectedmsg = `Client initialized with old header 85 (trusted is more recent) old=42 trustedHeight="forty two" 86 trustedHash=74657374206D65` 87 if strings.EqualFold(receivedmsg, expectedmsg) { 88 t.Fatalf("received %s, expected %s", receivedmsg, expectedmsg) 89 } 90 } 91 92 func BenchmarkTMLoggerSimple(b *testing.B) { 93 benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage) 94 } 95 96 func BenchmarkTMLoggerContextual(b *testing.B) { 97 benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage) 98 } 99 100 func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) { 101 lc := logger.With("common_key", "common_value") 102 b.ReportAllocs() 103 b.ResetTimer() 104 for i := 0; i < b.N; i++ { 105 f(lc) 106 } 107 } 108 109 var ( 110 baseInfoMessage = func(logger log.Logger) { logger.Info("foo_message", "foo_key", "foo_value") } 111 withInfoMessage = func(logger log.Logger) { logger.With("a", "b").Info("c", "d", "f") } 112 )