internal/debug: add switch to format logs with json (#22207)

adds a flag --log.json which if enabled makes the client format logs with JSON.
This commit is contained in:
Sina Mahmoodi 2021-02-09 10:42:55 +01:00 committed by GitHub
parent 2fdba3aacb
commit 27786671d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 12 deletions

View File

@ -41,6 +41,10 @@ var (
Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail", Usage: "Logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
Value: 3, Value: 3,
} }
logjsonFlag = cli.BoolFlag{
Name: "log.json",
Usage: "Format logs with JSON",
}
vmoduleFlag = cli.StringFlag{ vmoduleFlag = cli.StringFlag{
Name: "vmodule", Name: "vmodule",
Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)", Usage: "Per-module verbosity: comma-separated list of <pattern>=<level> (e.g. eth/*=5,p2p=4)",
@ -114,7 +118,7 @@ var (
// Flags holds all command-line flags required for debugging. // Flags holds all command-line flags required for debugging.
var Flags = []cli.Flag{ var Flags = []cli.Flag{
verbosityFlag, vmoduleFlag, backtraceAtFlag, debugFlag, verbosityFlag, logjsonFlag, vmoduleFlag, backtraceAtFlag, debugFlag,
pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag, pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag,
blockprofilerateFlag, cpuprofileFlag, traceFlag, blockprofilerateFlag, cpuprofileFlag, traceFlag,
} }
@ -124,24 +128,29 @@ var DeprecatedFlags = []cli.Flag{
legacyBlockprofilerateFlag, legacyCpuprofileFlag, legacyBlockprofilerateFlag, legacyCpuprofileFlag,
} }
var ( var glogger *log.GlogHandler
ostream log.Handler
glogger *log.GlogHandler
)
func init() { func init() {
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
output := io.Writer(os.Stderr) glogger.Verbosity(log.LvlInfo)
if usecolor { log.Root().SetHandler(glogger)
output = colorable.NewColorableStderr()
}
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
glogger = log.NewGlogHandler(ostream)
} }
// 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 {
var ostream log.Handler
output := io.Writer(os.Stderr)
if ctx.GlobalBool(logjsonFlag.Name) {
ostream = log.StreamHandler(output, log.JSONFormat())
} else {
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
if usecolor {
output = colorable.NewColorableStderr()
}
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
}
glogger.SetHandler(ostream)
// 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)))