gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/libs/log/tmfmt_logger_test.go (about)

     1  package log_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io/ioutil"
     7  	"math"
     8  	"regexp"
     9  	"testing"
    10  
    11  	kitlog "github.com/go-kit/log"
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"github.com/tendermint/tendermint/libs/log"
    15  )
    16  
    17  func TestTMFmtLogger(t *testing.T) {
    18  	t.Parallel()
    19  	buf := &bytes.Buffer{}
    20  	logger := log.NewTMFmtLogger(buf)
    21  
    22  	if err := logger.Log("hello", "world"); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ hello=world\n$`), buf.String())
    26  
    27  	buf.Reset()
    28  	if err := logger.Log("a", 1, "err", errors.New("error")); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ a=1 err=error\n$`), buf.String())
    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  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ std_map=map\[1:2\] my_map=special_behavior\n$`), buf.String())
    38  
    39  	buf.Reset()
    40  	if err := logger.Log("level", "error"); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	assert.Regexp(t, regexp.MustCompile(`E\[.+\] unknown \s+\n$`), buf.String())
    44  
    45  	buf.Reset()
    46  	if err := logger.Log("_msg", "Hello"); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String())
    50  
    51  	buf.Reset()
    52  	if err := logger.Log("module", "main", "module", "crypto", "module", "wire"); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+module=wire\s+\n$`), buf.String())
    56  
    57  	buf.Reset()
    58  	if err := logger.Log("hash", []byte("test me")); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ hash=74657374206D65\n$`), buf.String())
    62  }
    63  
    64  func BenchmarkTMFmtLoggerSimple(b *testing.B) {
    65  	benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), baseMessage)
    66  }
    67  
    68  func BenchmarkTMFmtLoggerContextual(b *testing.B) {
    69  	benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), withMessage)
    70  }
    71  
    72  func TestTMFmtLoggerConcurrency(t *testing.T) {
    73  	t.Parallel()
    74  	testConcurrency(t, log.NewTMFmtLogger(ioutil.Discard), 10000)
    75  }
    76  
    77  func benchmarkRunnerKitlog(b *testing.B, logger kitlog.Logger, f func(kitlog.Logger)) {
    78  	lc := kitlog.With(logger, "common_key", "common_value")
    79  	b.ReportAllocs()
    80  	b.ResetTimer()
    81  	for i := 0; i < b.N; i++ {
    82  		f(lc)
    83  	}
    84  }
    85  
    86  //nolint: errcheck // ignore errors
    87  var (
    88  	baseMessage = func(logger kitlog.Logger) { logger.Log("foo_key", "foo_value") }
    89  	withMessage = func(logger kitlog.Logger) { kitlog.With(logger, "a", "b").Log("d", "f") }
    90  )
    91  
    92  // These test are designed to be run with the race detector.
    93  
    94  func testConcurrency(t *testing.T, logger kitlog.Logger, total int) {
    95  	n := int(math.Sqrt(float64(total)))
    96  	share := total / n
    97  
    98  	errC := make(chan error, n)
    99  
   100  	for i := 0; i < n; i++ {
   101  		go func() {
   102  			errC <- spam(logger, share)
   103  		}()
   104  	}
   105  
   106  	for i := 0; i < n; i++ {
   107  		err := <-errC
   108  		if err != nil {
   109  			t.Fatalf("concurrent logging error: %v", err)
   110  		}
   111  	}
   112  }
   113  
   114  func spam(logger kitlog.Logger, count int) error {
   115  	for i := 0; i < count; i++ {
   116  		err := logger.Log("key", i)
   117  		if err != nil {
   118  			return err
   119  		}
   120  	}
   121  	return nil
   122  }
   123  
   124  type mymap map[int]int
   125  
   126  func (m mymap) String() string { return "special_behavior" }