Skip to content

Commit

Permalink
Support geth logging to stdout and in JSON format (ethereum#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmoreton authored Oct 30, 2018
1 parent 0d0b6fb commit 73a7e9a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
)
flag.Parse()

glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
glogger := log.NewGlogHandler(log.StreamHandler(os.Stdout, log.JSONFormat()))
glogger.Verbosity(log.Lvl(*verbosity))
glogger.Vmodule(*vmodule)
log.Root().SetHandler(glogger)
Expand Down
41 changes: 35 additions & 6 deletions internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,22 @@ var (
Name: "trace",
Usage: "Write execution trace to the given file",
}
consoleFormatFlag = cli.StringFlag{
Name: "consoleformat",
Usage: "Write console logs as 'json' or 'term'",
}
stdoutFlag = cli.BoolFlag{
Name: "stdout",
Usage: "Write console logs to stdout (not stderr)",
}
)

// Flags holds all command-line flags required for debugging.
var Flags = []cli.Flag{
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
pprofFlag, pprofAddrFlag, pprofPortFlag,
memprofilerateFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
consoleFormatFlag, stdoutFlag,
}

var (
Expand All @@ -101,19 +110,39 @@ var (
)

func init() {
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
output := io.Writer(os.Stderr)
if usecolor {
output = colorable.NewColorableStderr()
}
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
ostream = log.StreamHandler(io.Writer(os.Stderr), log.TerminalFormat(false))
glogger = log.NewGlogHandler(ostream)
}

// 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, logdir string) error {
// logging

var output io.Writer
var usecolor bool

if ctx.GlobalBool(stdoutFlag.Name) {
output = io.Writer(os.Stdout)
usecolor = (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb"
if usecolor {
output = colorable.NewColorableStdout()
}
} else {
output = io.Writer(os.Stderr)
usecolor = (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
if usecolor {
output = colorable.NewColorableStderr()
}
}

if consoleFormat := ctx.GlobalString(consoleFormatFlag.Name); consoleFormat == "json" {
ostream = log.StreamHandler(output, log.JSONFormat())
} else {
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
}
glogger = log.NewGlogHandler(ostream)

log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
if logdir != "" {
rfh, err := log.RotatingFileHandler(
Expand Down

0 comments on commit 73a7e9a

Please sign in to comment.