github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/libs/cli/flags/log_level_test.go (about)

     1  package flags_test
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	tmflags "github.com/pure-x-eth/consensus_tm/libs/cli/flags"
     9  	"github.com/pure-x-eth/consensus_tm/libs/log"
    10  )
    11  
    12  const (
    13  	defaultLogLevelValue = "info"
    14  )
    15  
    16  func TestParseLogLevel(t *testing.T) {
    17  	var buf bytes.Buffer
    18  	jsonLogger := log.NewTMJSONLoggerNoTS(&buf)
    19  
    20  	correctLogLevels := []struct {
    21  		lvl              string
    22  		expectedLogLines []string
    23  	}{
    24  		{"mempool:error", []string{
    25  			``, // if no default is given, assume info
    26  			``,
    27  			`{"_msg":"Mesmero","level":"error","module":"mempool"}`,
    28  			`{"_msg":"Mind","level":"info","module":"state"}`, // if no default is given, assume info
    29  			``}},
    30  
    31  		{"mempool:error,*:debug", []string{
    32  			`{"_msg":"Kingpin","level":"debug","module":"wire"}`,
    33  			``,
    34  			`{"_msg":"Mesmero","level":"error","module":"mempool"}`,
    35  			`{"_msg":"Mind","level":"info","module":"state"}`,
    36  			`{"_msg":"Gideon","level":"debug"}`}},
    37  
    38  		{"*:debug,wire:none", []string{
    39  			``,
    40  			`{"_msg":"Kitty Pryde","level":"info","module":"mempool"}`,
    41  			`{"_msg":"Mesmero","level":"error","module":"mempool"}`,
    42  			`{"_msg":"Mind","level":"info","module":"state"}`,
    43  			`{"_msg":"Gideon","level":"debug"}`}},
    44  	}
    45  
    46  	for _, c := range correctLogLevels {
    47  		logger, err := tmflags.ParseLogLevel(c.lvl, jsonLogger, defaultLogLevelValue)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		buf.Reset()
    53  
    54  		logger.With("module", "mempool").With("module", "wire").Debug("Kingpin")
    55  		if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have {
    56  			t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl)
    57  		}
    58  
    59  		buf.Reset()
    60  
    61  		logger.With("module", "mempool").Info("Kitty Pryde")
    62  		if have := strings.TrimSpace(buf.String()); c.expectedLogLines[1] != have {
    63  			t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[1], have, c.lvl)
    64  		}
    65  
    66  		buf.Reset()
    67  
    68  		logger.With("module", "mempool").Error("Mesmero")
    69  		if have := strings.TrimSpace(buf.String()); c.expectedLogLines[2] != have {
    70  			t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[2], have, c.lvl)
    71  		}
    72  
    73  		buf.Reset()
    74  
    75  		logger.With("module", "state").Info("Mind")
    76  		if have := strings.TrimSpace(buf.String()); c.expectedLogLines[3] != have {
    77  			t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[3], have, c.lvl)
    78  		}
    79  
    80  		buf.Reset()
    81  
    82  		logger.Debug("Gideon")
    83  		if have := strings.TrimSpace(buf.String()); c.expectedLogLines[4] != have {
    84  			t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[4], have, c.lvl)
    85  		}
    86  	}
    87  
    88  	incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"}
    89  	for _, lvl := range incorrectLogLevel {
    90  		if _, err := tmflags.ParseLogLevel(lvl, jsonLogger, defaultLogLevelValue); err == nil {
    91  			t.Fatalf("Expected %s to produce error", lvl)
    92  		}
    93  	}
    94  }