internal/debug: add JSON log format and rename logging flags (#22341)

This change adds support for logging JSON records when the --log.json flag is
given. The --debug and --backtrace flags are deprecated and replaced by
--log.debug and --log.backtrace.

While changing this, it was noticed that the --memprofilerate and
--blockprofilerate were ineffective (they were always overridden even if
--pprof.memprofilerate was not set). This is also fixed.

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Sina Mahmoodi 2021-04-06 18:39:51 +04:30 committed by GitHub
parent adf09aeab1
commit 5338ce4447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,22 +41,22 @@ 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)",
Value: "", Value: "",
} }
logjsonFlag = cli.BoolFlag{
Name: "log.json",
Usage: "Format logs with JSON",
}
backtraceAtFlag = cli.StringFlag{ backtraceAtFlag = cli.StringFlag{
Name: "backtrace", Name: "log.backtrace",
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")", Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
Value: "", Value: "",
} }
debugFlag = cli.BoolFlag{ debugFlag = cli.BoolFlag{
Name: "debug", Name: "log.debug",
Usage: "Prepends log messages with call-site location (file and line number)", Usage: "Prepends log messages with call-site location (file and line number)",
} }
pprofFlag = cli.BoolFlag{ pprofFlag = cli.BoolFlag{
@ -90,18 +90,69 @@ var (
Name: "trace", Name: "trace",
Usage: "Write execution trace to the given file", Usage: "Write execution trace to the given file",
} }
// (Deprecated April 2020)
legacyPprofPortFlag = cli.IntFlag{
Name: "pprofport",
Usage: "pprof HTTP server listening port (deprecated, use --pprof.port)",
Value: 6060,
}
legacyPprofAddrFlag = cli.StringFlag{
Name: "pprofaddr",
Usage: "pprof HTTP server listening interface (deprecated, use --pprof.addr)",
Value: "127.0.0.1",
}
legacyMemprofilerateFlag = cli.IntFlag{
Name: "memprofilerate",
Usage: "Turn on memory profiling with the given rate (deprecated, use --pprof.memprofilerate)",
Value: runtime.MemProfileRate,
}
legacyBlockprofilerateFlag = cli.IntFlag{
Name: "blockprofilerate",
Usage: "Turn on block profiling with the given rate (deprecated, use --pprof.blockprofilerate)",
}
legacyCpuprofileFlag = cli.StringFlag{
Name: "cpuprofile",
Usage: "Write CPU profile to the given file (deprecated, use --pprof.cpuprofile)",
}
legacyBacktraceAtFlag = cli.StringFlag{
Name: "backtrace",
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\") (deprecated, use --log.backtrace)",
Value: "",
}
legacyDebugFlag = cli.BoolFlag{
Name: "debug",
Usage: "Prepends log messages with call-site location (file and line number) (deprecated, use --log.debug)",
}
) )
// 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, logjsonFlag, vmoduleFlag, backtraceAtFlag, debugFlag, verbosityFlag,
pprofFlag, pprofAddrFlag, pprofPortFlag, memprofilerateFlag, vmoduleFlag,
blockprofilerateFlag, cpuprofileFlag, traceFlag, logjsonFlag,
backtraceAtFlag,
debugFlag,
pprofFlag,
pprofAddrFlag,
pprofPortFlag,
memprofilerateFlag,
blockprofilerateFlag,
cpuprofileFlag,
traceFlag,
} }
var ( // This is the list of deprecated debugging flags.
glogger *log.GlogHandler var DeprecatedFlags = []cli.Flag{
) legacyPprofPortFlag,
legacyPprofAddrFlag,
legacyMemprofilerateFlag,
legacyBlockprofilerateFlag,
legacyCpuprofileFlag,
legacyBacktraceAtFlag,
legacyDebugFlag,
}
var glogger *log.GlogHandler
func init() { func init() {
glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
@ -124,17 +175,54 @@ func Setup(ctx *cli.Context) error {
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor)) ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
} }
glogger.SetHandler(ostream) glogger.SetHandler(ostream)
// logging // logging
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name)) verbosity := ctx.GlobalInt(verbosityFlag.Name)
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) glogger.Verbosity(log.Lvl(verbosity))
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name)) vmodule := ctx.GlobalString(vmoduleFlag.Name)
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name)) glogger.Vmodule(vmodule)
debug := ctx.GlobalBool(debugFlag.Name)
if ctx.GlobalIsSet(legacyDebugFlag.Name) {
debug = ctx.GlobalBool(legacyDebugFlag.Name)
log.Warn("The flag --debug is deprecated and will be removed in the future, please use --log.debug")
}
if ctx.GlobalIsSet(debugFlag.Name) {
debug = ctx.GlobalBool(debugFlag.Name)
}
log.PrintOrigins(debug)
backtrace := ctx.GlobalString(backtraceAtFlag.Name)
if b := ctx.GlobalString(legacyBacktraceAtFlag.Name); b != "" {
backtrace = b
log.Warn("The flag --backtrace is deprecated and will be removed in the future, please use --log.backtrace")
}
if b := ctx.GlobalString(backtraceAtFlag.Name); b != "" {
backtrace = b
}
glogger.BacktraceAt(backtrace)
log.Root().SetHandler(glogger) log.Root().SetHandler(glogger)
// profiling, tracing // profiling, tracing
runtime.MemProfileRate = memprofilerateFlag.Value
if ctx.GlobalIsSet(legacyMemprofilerateFlag.Name) {
runtime.MemProfileRate = ctx.GlobalInt(legacyMemprofilerateFlag.Name)
log.Warn("The flag --memprofilerate is deprecated and will be removed in the future, please use --pprof.memprofilerate")
}
if ctx.GlobalIsSet(memprofilerateFlag.Name) {
runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name) runtime.MemProfileRate = ctx.GlobalInt(memprofilerateFlag.Name)
}
Handler.SetBlockProfileRate(ctx.GlobalInt(blockprofilerateFlag.Name)) blockProfileRate := blockprofilerateFlag.Value
if ctx.GlobalIsSet(legacyBlockprofilerateFlag.Name) {
blockProfileRate = ctx.GlobalInt(legacyBlockprofilerateFlag.Name)
log.Warn("The flag --blockprofilerate is deprecated and will be removed in the future, please use --pprof.blockprofilerate")
}
if ctx.GlobalIsSet(blockprofilerateFlag.Name) {
blockProfileRate = ctx.GlobalInt(blockprofilerateFlag.Name)
}
Handler.SetBlockProfileRate(blockProfileRate)
if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" { if traceFile := ctx.GlobalString(traceFlag.Name); traceFile != "" {
if err := Handler.StartGoTrace(traceFile); err != nil { if err := Handler.StartGoTrace(traceFile); err != nil {