Merge pull request #3708 from fjl/log-letter

log: fix annoyances
This commit is contained in:
Péter Szilágyi 2017-02-27 18:05:50 +02:00 committed by GitHub
commit 48bc07ae97
13 changed files with 64 additions and 45 deletions

View File

@ -50,7 +50,7 @@ func main() {
) )
flag.Parse() 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.Verbosity(log.Lvl(*verbosity))
glogger.Vmodule(*vmodule) glogger.Vmodule(*vmodule)
log.Root().SetHandler(glogger) log.Root().SetHandler(glogger)

View File

@ -112,7 +112,7 @@ func init() {
} }
func run(ctx *cli.Context) error { 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))) glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
log.Root().SetHandler(glogger) log.Root().SetHandler(glogger)

View File

@ -152,7 +152,7 @@ func echo() {
} }
func initialize() { 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{}) done = make(chan struct{})
var peers []*discover.Node var peers []*discover.Node

View File

@ -30,7 +30,7 @@ import (
) )
func init() { 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") var testAccount, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")

View File

@ -18,12 +18,15 @@ package debug
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"runtime" "runtime"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log/term"
colorable "github.com/mattn/go-colorable"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
@ -87,16 +90,22 @@ var Flags = []cli.Flag{
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag, memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
} }
// glogger is the glog handler used by Geth, allowing the debug APIs to modify var glogger *log.GlogHandler
// verbosity levels, vmodules and backtrace locations.
var glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat())) 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. // Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program. // It should be called as early as possible in the program.
func Setup(ctx *cli.Context) error { func Setup(ctx *cli.Context) error {
// logging // logging
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name)) log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name)) glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name)) glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))

View File

@ -69,26 +69,28 @@ func (f formatFunc) Format(r *Record) []byte {
// //
// [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002 // [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 { return FormatFunc(func(r *Record) []byte {
var color = 0 var color = 0
switch r.Lvl { if usecolor {
case LvlCrit: switch r.Lvl {
color = 35 case LvlCrit:
case LvlError: color = 35
color = 31 case LvlError:
case LvlWarn: color = 31
color = 33 case LvlWarn:
case LvlInfo: color = 33
color = 32 case LvlInfo:
case LvlDebug: color = 32
color = 36 case LvlDebug:
case LvlTrace: color = 36
color = 34 case LvlTrace:
color = 34
}
} }
b := &bytes.Buffer{} b := &bytes.Buffer{}
lvl := strings.ToUpper(r.Lvl.String()) lvl := r.Lvl.AlignedString()
if atomic.LoadUint32(&locationEnabled) != 0 { if atomic.LoadUint32(&locationEnabled) != 0 {
// Log origin printing was requested, format the location path and line number // Log origin printing was requested, format the location path and line number
location := fmt.Sprintf("%+v", r.Call) location := fmt.Sprintf("%+v", r.Call)
@ -107,13 +109,13 @@ func TerminalFormat() Format {
if color > 0 { 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) fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
} else { } 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 { } else {
if color > 0 { if color > 0 {
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg) fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
} else { } 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 // try to justify the log output for short messages

View File

@ -24,7 +24,27 @@ const (
LvlTrace 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 { func (l Lvl) String() string {
switch l { switch l {
case LvlTrace: case LvlTrace:

View File

@ -2,28 +2,16 @@ package log
import ( import (
"os" "os"
"github.com/ethereum/go-ethereum/log/term"
"github.com/mattn/go-colorable"
) )
var ( var (
root *logger root = &logger{[]interface{}{}, new(swapHandler)}
StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
StderrHandler = StreamHandler(os.Stderr, LogfmtFormat()) StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
) )
func init() { func init() {
if term.IsTty(os.Stdout.Fd()) { root.SetHandler(DiscardHandler())
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))
} }
// New returns a new logger with the given context. // New returns a new logger with the given context.

View File

@ -27,7 +27,7 @@ import (
func init() { func init() {
// Initialize the logger // 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 // Initialize the goroutine count
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -24,5 +24,5 @@ import (
// SetVerbosity sets the global verbosity level (between 0 and 6 - see logger/verbosity.go). // SetVerbosity sets the global verbosity level (between 0 and 6 - see logger/verbosity.go).
func SetVerbosity(level int) { 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))))
} }

View File

@ -31,7 +31,7 @@ import (
) )
func init() { 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 { type testTransport struct {

View File

@ -31,7 +31,7 @@ import (
) )
func init() { 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 { type testSyncDb struct {

View File

@ -41,7 +41,7 @@ var (
) )
func init() { 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" { if os.Getenv("JITVM") == "true" {
ForceJit = true ForceJit = true
EnableJit = true EnableJit = true