github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/chains/tm34/libs/log/tracing_logger_test.go (about) 1 package log_test 2 3 import ( 4 "bytes" 5 stderr "errors" 6 "fmt" 7 "strings" 8 "testing" 9 10 "github.com/pkg/errors" 11 12 "github.com/supragya/TendermintConnector/chains/tm34/libs/log" 13 ) 14 15 func TestTracingLogger(t *testing.T) { 16 var buf bytes.Buffer 17 18 logger := log.NewTMJSONLogger(&buf) 19 20 logger1 := log.NewTracingLogger(logger) 21 err1 := errors.New("courage is grace under pressure") 22 err2 := errors.New("it does not matter how slowly you go, so long as you do not stop") 23 logger1.With("err1", err1).Info("foo", "err2", err2) 24 25 want := strings.ReplaceAll( 26 strings.ReplaceAll( 27 `{"_msg":"foo","err1":"`+ 28 fmt.Sprintf("%+v", err1)+ 29 `","err2":"`+ 30 fmt.Sprintf("%+v", err2)+ 31 `","level":"info"}`, 32 "\t", "", 33 ), "\n", "") 34 have := strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(buf.String()), "\\n", ""), "\\t", "") 35 if want != have { 36 t.Errorf("\nwant '%s'\nhave '%s'", want, have) 37 } 38 39 buf.Reset() 40 41 logger.With( 42 "err1", stderr.New("opportunities don't happen. You create them"), 43 ).Info( 44 "foo", "err2", stderr.New("once you choose hope, anything's possible"), 45 ) 46 47 want = `{"_msg":"foo",` + 48 `"err1":"opportunities don't happen. You create them",` + 49 `"err2":"once you choose hope, anything's possible",` + 50 `"level":"info"}` 51 have = strings.TrimSpace(buf.String()) 52 if want != have { 53 t.Errorf("\nwant '%s'\nhave '%s'", want, have) 54 } 55 56 buf.Reset() 57 58 logger.With("user", "Sam").With("context", "value").Info("foo", "bar", "baz") 59 60 want = `{"_msg":"foo","bar":"baz","context":"value","level":"info","user":"Sam"}` 61 have = strings.TrimSpace(buf.String()) 62 if want != have { 63 t.Errorf("\nwant '%s'\nhave '%s'", want, have) 64 } 65 }