mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-15 08:01:19 +00:00
changes per Frey comments (Refs #493)
This commit is contained in:
parent
05a8204508
commit
fb0df75de0
@ -14,22 +14,24 @@ const (
|
|||||||
defaultLogLevelKey = "*"
|
defaultLogLevelKey = "*"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsLogLevelSimple returns true if log level is a single word ("info", ...).
|
// ParseLogLevel parses complex log level - comma-separated
|
||||||
func IsLogLevelSimple(l string) bool {
|
|
||||||
return !strings.Contains(l, ":")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseComplexLogLevel parses complex log level - comma-separated
|
|
||||||
// list of module:level pairs with an optional *:level pair (* means
|
// list of module:level pairs with an optional *:level pair (* means
|
||||||
// all other modules).
|
// all other modules).
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
// ParseComplexLogLevel("consensus:debug,mempool:debug,*:error", log.NewTMLogger(os.Stdout))
|
// ParseLogLevel("consensus:debug,mempool:debug,*:error", log.NewTMLogger(os.Stdout))
|
||||||
func ParseComplexLogLevel(l string, logger log.Logger) (log.Logger, error) {
|
func ParseLogLevel(lvl string, logger log.Logger) (log.Logger, error) {
|
||||||
if l == "" {
|
if lvl == "" {
|
||||||
return nil, errors.New("Empty log level")
|
return nil, errors.New("Empty log level")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l := lvl
|
||||||
|
|
||||||
|
// prefix simple one word levels (e.g. "info") with "*"
|
||||||
|
if !strings.Contains(l, ":") {
|
||||||
|
l = defaultLogLevelKey + ":" + l
|
||||||
|
}
|
||||||
|
|
||||||
options := make([]log.Option, 0)
|
options := make([]log.Option, 0)
|
||||||
|
|
||||||
isDefaultLogLevelSet := false
|
isDefaultLogLevelSet := false
|
||||||
|
@ -1,41 +1,63 @@
|
|||||||
package flags_test
|
package flags_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
tmflags "github.com/tendermint/tendermint/cmd/tendermint/commands/flags"
|
tmflags "github.com/tendermint/tendermint/cmd/tendermint/commands/flags"
|
||||||
"github.com/tendermint/tmlibs/log"
|
"github.com/tendermint/tmlibs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsLogLevelSimple(t *testing.T) {
|
func TestParseLogLevel(t *testing.T) {
|
||||||
simpleFlags := []string{"info", "debug", "some"}
|
var buf bytes.Buffer
|
||||||
for _, f := range simpleFlags {
|
jsonLogger := log.NewTMJSONLogger(&buf)
|
||||||
if !tmflags.IsLogLevelSimple(f) {
|
|
||||||
t.Fatalf("%s is a simple flag", f)
|
correctLogLevels := []struct {
|
||||||
}
|
lvl string
|
||||||
|
expectedLogLines []string
|
||||||
|
}{
|
||||||
|
{"mempool:error", []string{``, ``, `{"_msg":"Mesmero","level":"error","module":"mempool"}`}},
|
||||||
|
{"mempool:error,*:debug", []string{``, ``, `{"_msg":"Mesmero","level":"error","module":"mempool"}`}},
|
||||||
|
{"*:debug,wire:none", []string{
|
||||||
|
`{"_msg":"Kingpin","level":"debug","module":"mempool"}`,
|
||||||
|
`{"_msg":"Kitty Pryde","level":"info","module":"mempool"}`,
|
||||||
|
`{"_msg":"Mesmero","level":"error","module":"mempool"}`}},
|
||||||
}
|
}
|
||||||
|
|
||||||
complexFlags := []string{"mempool:error", "mempool:error,*:debug"}
|
for _, c := range correctLogLevels {
|
||||||
for _, f := range complexFlags {
|
logger, err := tmflags.ParseLogLevel(c.lvl, jsonLogger)
|
||||||
if tmflags.IsLogLevelSimple(f) {
|
if err != nil {
|
||||||
t.Fatalf("%s is a complex flag", f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseComplexLogLevel(t *testing.T) {
|
|
||||||
logger := log.TestingLogger()
|
|
||||||
|
|
||||||
correctLogLevels := []string{"mempool:error", "mempool:error,*:debug", "*:debug,wire:none"}
|
|
||||||
for _, lvl := range correctLogLevels {
|
|
||||||
if _, err := tmflags.ParseComplexLogLevel(lvl, logger); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger = logger.With("module", "mempool")
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
logger.Debug("Kingpin")
|
||||||
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have {
|
||||||
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
logger.Info("Kitty Pryde")
|
||||||
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[1] != have {
|
||||||
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[1], have, c.lvl)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
logger.Error("Mesmero")
|
||||||
|
if have := strings.TrimSpace(buf.String()); c.expectedLogLines[2] != have {
|
||||||
|
t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[2], have, c.lvl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"}
|
incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"}
|
||||||
for _, lvl := range incorrectLogLevel {
|
for _, lvl := range incorrectLogLevel {
|
||||||
if _, err := tmflags.ParseComplexLogLevel(lvl, logger); err == nil {
|
if _, err := tmflags.ParseLogLevel(lvl, jsonLogger); err == nil {
|
||||||
t.Fatalf("Expected %s to produce error", lvl)
|
t.Fatalf("Expected %s to produce error", lvl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -26,24 +25,15 @@ var RootCmd = &cobra.Command{
|
|||||||
Short: "Tendermint Core (BFT Consensus) in Go",
|
Short: "Tendermint Core (BFT Consensus) in Go",
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := viper.Unmarshal(config)
|
err := viper.Unmarshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
config.SetRoot(config.RootDir)
|
config.SetRoot(config.RootDir)
|
||||||
cfg.EnsureRoot(config.RootDir)
|
cfg.EnsureRoot(config.RootDir)
|
||||||
if tmflags.IsLogLevelSimple(config.LogLevel) {
|
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger)
|
||||||
var option log.Option
|
if err != nil {
|
||||||
switch config.LogLevel {
|
|
||||||
case "info":
|
|
||||||
option = log.AllowInfo()
|
|
||||||
case "debug":
|
|
||||||
option = log.AllowDebug()
|
|
||||||
case "error":
|
|
||||||
option = log.AllowError()
|
|
||||||
case "none":
|
|
||||||
option = log.AllowNone()
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("Expected log level to be either \"info\", \"debug\", \"error\" or \"none\", given %s", config.LogLevel)
|
|
||||||
}
|
|
||||||
logger = log.NewFilter(logger, option)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
tmflags "github.com/tendermint/tendermint/cmd/tendermint/commands/flags"
|
|
||||||
"github.com/tendermint/tendermint/node"
|
"github.com/tendermint/tendermint/node"
|
||||||
"github.com/tendermint/tendermint/types"
|
"github.com/tendermint/tendermint/types"
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
@ -80,14 +79,6 @@ func runNode(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !tmflags.IsLogLevelSimple(config.LogLevel) {
|
|
||||||
var err error
|
|
||||||
logger, err = tmflags.ParseComplexLogLevel(config.LogLevel, logger)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create & start node
|
// Create & start node
|
||||||
n := node.NewNodeDefault(config, logger.With("module", "node"))
|
n := node.NewNodeDefault(config, logger.With("module", "node"))
|
||||||
if _, err := n.Start(); err != nil {
|
if _, err := n.Start(); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user