From 38e273597c283e607fd58199ed77890813f7c22c Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Feb 2017 18:30:32 +0100 Subject: [PATCH 1/3] log: log full level names instead of mispelled "EROR", "DBUG" --- log/format.go | 6 +++--- log/logger.go | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/log/format.go b/log/format.go index 2a3790501..c61a4d82a 100644 --- a/log/format.go +++ b/log/format.go @@ -88,7 +88,7 @@ func TerminalFormat() Format { } b := &bytes.Buffer{} - lvl := strings.ToUpper(r.Lvl.String()) + lvl := r.Lvl.AlignedString() if atomic.LoadUint32(&locationEnabled) != 0 { // Log origin printing was requested, format the location path and line number location := fmt.Sprintf("%+v", r.Call) @@ -107,13 +107,13 @@ func TerminalFormat() Format { if color > 0 { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg) } else { - fmt.Fprintf(b, "[%s] [%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg) + fmt.Fprintf(b, "%s[%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg) } } else { if color > 0 { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg) } else { - fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg) + fmt.Fprintf(b, "%s[%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg) } } // try to justify the log output for short messages diff --git a/log/logger.go b/log/logger.go index a7f7d9df7..15c83a9b2 100644 --- a/log/logger.go +++ b/log/logger.go @@ -24,7 +24,27 @@ const ( LvlTrace ) -// Returns the name of a Lvl +// Aligned returns a 5-character string containing the name of a Lvl. +func (l Lvl) AlignedString() string { + switch l { + case LvlTrace: + return "TRACE" + case LvlDebug: + return "DEBUG" + case LvlInfo: + return "INFO " + case LvlWarn: + return "WARN " + case LvlError: + return "ERROR" + case LvlCrit: + return "CRIT " + default: + panic("bad level") + } +} + +// Strings returns the name of a Lvl. func (l Lvl) String() string { switch l { case LvlTrace: From 43362ab4fb561323e1a766405eaa8f82dec4a280 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Feb 2017 18:42:42 +0100 Subject: [PATCH 2/3] log: disable logs by default --- log/root.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/log/root.go b/log/root.go index 12afbf8b7..71b8cef6d 100644 --- a/log/root.go +++ b/log/root.go @@ -2,28 +2,16 @@ package log import ( "os" - - "github.com/ethereum/go-ethereum/log/term" - "github.com/mattn/go-colorable" ) var ( - root *logger + root = &logger{[]interface{}{}, new(swapHandler)} StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) StderrHandler = StreamHandler(os.Stderr, LogfmtFormat()) ) func init() { - if term.IsTty(os.Stdout.Fd()) { - StdoutHandler = StreamHandler(colorable.NewColorableStdout(), TerminalFormat()) - } - - if term.IsTty(os.Stderr.Fd()) { - StderrHandler = StreamHandler(colorable.NewColorableStderr(), TerminalFormat()) - } - - root = &logger{[]interface{}{}, new(swapHandler)} - root.SetHandler(LvlFilterHandler(LvlInfo, StdoutHandler)) + root.SetHandler(DiscardHandler()) } // New returns a new logger with the given context. From d0eba23af373bb54b00b242ccee4239fc9afd873 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 23 Feb 2017 19:31:13 +0100 Subject: [PATCH 3/3] all: disable log message colors outside of geth Also tweak behaviour so colors are only enabled when stderr is a terminal. --- cmd/bootnode/main.go | 2 +- cmd/evm/main.go | 2 +- cmd/wnode/main.go | 2 +- eth/protocol_test.go | 2 +- internal/debug/flags.go | 17 +++++++++++++---- log/format.go | 30 ++++++++++++++++-------------- mobile/init.go | 2 +- mobile/logger.go | 2 +- p2p/server_test.go | 2 +- swarm/network/syncdb_test.go | 2 +- tests/util.go | 2 +- 11 files changed, 38 insertions(+), 27 deletions(-) diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index c3fc163a1..a5e82d145 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -50,7 +50,7 @@ func main() { ) flag.Parse() - glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat())) + glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) glogger.Verbosity(log.Lvl(*verbosity)) glogger.Vmodule(*vmodule) log.Root().SetHandler(glogger) diff --git a/cmd/evm/main.go b/cmd/evm/main.go index a489efbfc..601e62f77 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -112,7 +112,7 @@ func init() { } func run(ctx *cli.Context) error { - glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat())) + glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name))) log.Root().SetHandler(glogger) diff --git a/cmd/wnode/main.go b/cmd/wnode/main.go index 8191f9292..82d7eda3c 100644 --- a/cmd/wnode/main.go +++ b/cmd/wnode/main.go @@ -152,7 +152,7 @@ func echo() { } func initialize() { - log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*argVerbosity), log.StreamHandler(os.Stderr, log.TerminalFormat()))) + log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*argVerbosity), log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) done = make(chan struct{}) var peers []*discover.Node diff --git a/eth/protocol_test.go b/eth/protocol_test.go index c0458f2be..43f336c32 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -30,7 +30,7 @@ import ( ) func init() { - // log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat()))) + // log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) } var testAccount, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") diff --git a/internal/debug/flags.go b/internal/debug/flags.go index 29d1f3388..f95251939 100644 --- a/internal/debug/flags.go +++ b/internal/debug/flags.go @@ -18,12 +18,15 @@ package debug import ( "fmt" + "io" "net/http" _ "net/http/pprof" "os" "runtime" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/log/term" + colorable "github.com/mattn/go-colorable" "gopkg.in/urfave/cli.v1" ) @@ -87,16 +90,22 @@ var Flags = []cli.Flag{ memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag, } -// glogger is the glog handler used by Geth, allowing the debug APIs to modify -// verbosity levels, vmodules and backtrace locations. -var glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat())) +var glogger *log.GlogHandler + +func init() { + usecolor := term.IsTty(os.Stderr.Fd()) && os.Getenv("TERM") != "dumb" + output := io.Writer(os.Stderr) + if usecolor { + output = colorable.NewColorableStderr() + } + glogger = log.NewGlogHandler(log.StreamHandler(output, log.TerminalFormat(usecolor))) +} // Setup initializes profiling and logging based on the CLI flags. // It should be called as early as possible in the program. func Setup(ctx *cli.Context) error { // logging log.PrintOrigins(ctx.GlobalBool(debugFlag.Name)) - glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name)) glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name)) diff --git a/log/format.go b/log/format.go index c61a4d82a..f32fcf744 100644 --- a/log/format.go +++ b/log/format.go @@ -69,22 +69,24 @@ func (f formatFunc) Format(r *Record) []byte { // // [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002 // -func TerminalFormat() Format { +func TerminalFormat(usecolor bool) Format { return FormatFunc(func(r *Record) []byte { var color = 0 - switch r.Lvl { - case LvlCrit: - color = 35 - case LvlError: - color = 31 - case LvlWarn: - color = 33 - case LvlInfo: - color = 32 - case LvlDebug: - color = 36 - case LvlTrace: - color = 34 + if usecolor { + switch r.Lvl { + case LvlCrit: + color = 35 + case LvlError: + color = 31 + case LvlWarn: + color = 33 + case LvlInfo: + color = 32 + case LvlDebug: + color = 36 + case LvlTrace: + color = 34 + } } b := &bytes.Buffer{} diff --git a/mobile/init.go b/mobile/init.go index d7acc14ce..2025d85ed 100644 --- a/mobile/init.go +++ b/mobile/init.go @@ -27,7 +27,7 @@ import ( func init() { // Initialize the logger - log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat()))) + log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) // Initialize the goroutine count runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/mobile/logger.go b/mobile/logger.go index 9f24b3279..7078c4fd2 100644 --- a/mobile/logger.go +++ b/mobile/logger.go @@ -24,5 +24,5 @@ import ( // SetVerbosity sets the global verbosity level (between 0 and 6 - see logger/verbosity.go). func SetVerbosity(level int) { - log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(level), log.StreamHandler(os.Stderr, log.TerminalFormat()))) + log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(level), log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) } diff --git a/p2p/server_test.go b/p2p/server_test.go index 46611c7d0..971faf002 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -31,7 +31,7 @@ import ( ) func init() { - // log.Root().SetHandler(log.LvlFilterHandler(log.LvlError, log.StreamHandler(os.Stderr, log.TerminalFormat()))) + // log.Root().SetHandler(log.LvlFilterHandler(log.LvlError, log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) } type testTransport struct { diff --git a/swarm/network/syncdb_test.go b/swarm/network/syncdb_test.go index a9417e1d4..be21d156f 100644 --- a/swarm/network/syncdb_test.go +++ b/swarm/network/syncdb_test.go @@ -31,7 +31,7 @@ import ( ) func init() { - log.Root().SetHandler(log.LvlFilterHandler(log.LvlCrit, log.StreamHandler(os.Stderr, log.TerminalFormat()))) + log.Root().SetHandler(log.LvlFilterHandler(log.LvlCrit, log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) } type testSyncDb struct { diff --git a/tests/util.go b/tests/util.go index ce5b02fed..78bb06d06 100644 --- a/tests/util.go +++ b/tests/util.go @@ -41,7 +41,7 @@ var ( ) func init() { - log.Root().SetHandler(log.LvlFilterHandler(log.LvlCrit, log.StreamHandler(os.Stderr, log.TerminalFormat()))) + log.Root().SetHandler(log.LvlFilterHandler(log.LvlCrit, log.StreamHandler(os.Stderr, log.TerminalFormat(false)))) if os.Getenv("JITVM") == "true" { ForceJit = true EnableJit = true