github.com/ethereum/go-ethereum@v1.16.1/internal/testlog/testlog_test.go (about) 1 package testlog 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "strings" 8 "testing" 9 10 "github.com/ethereum/go-ethereum/log" 11 ) 12 13 type mockT struct { 14 out io.Writer 15 } 16 17 func (t *mockT) Helper() { 18 // noop for the purposes of unit tests 19 } 20 21 func (t *mockT) Logf(format string, args ...any) { 22 // we could gate this operation in a mutex, but because testlogger 23 // only calls Logf with its internal mutex held, we just write output here 24 var lineBuf bytes.Buffer 25 if _, err := fmt.Fprintf(&lineBuf, format, args...); err != nil { 26 panic(err) 27 } 28 // The timestamp is locale-dependent, so we want to trim that off 29 // "INFO [01-01|00:00:00.000] a message ..." -> "a message..." 30 sanitized := strings.Split(lineBuf.String(), "]")[1] 31 if _, err := t.out.Write([]byte(sanitized)); err != nil { 32 panic(err) 33 } 34 } 35 36 func TestLogging(t *testing.T) { 37 tests := []struct { 38 name string 39 expected string 40 run func(t *mockT) 41 }{ 42 { 43 "SubLogger", 44 ` Visible 45 Hide and seek foobar=123 46 Also visible 47 `, 48 func(t *mockT) { 49 l := Logger(t, log.LevelInfo) 50 subLogger := l.New("foobar", 123) 51 52 l.Info("Visible") 53 subLogger.Info("Hide and seek") 54 l.Info("Also visible") 55 }, 56 }, 57 } 58 59 for _, tc := range tests { 60 outp := bytes.Buffer{} 61 mock := mockT{&outp} 62 tc.run(&mock) 63 if outp.String() != tc.expected { 64 fmt.Printf("output mismatch.\nwant: '%s'\ngot: '%s'\n", tc.expected, outp.String()) 65 } 66 } 67 }