Skip to content

Commit

Permalink
internal/debug: add --log.file option (ethereum#26149)
Browse files Browse the repository at this point in the history
This adds an option to direct log output to a file. This feature has been
requested a lot. It's sometimes useful to have this available when running
geth in an environment that doesn't easily allow redirecting the output.

Notably, there is no support for log rotation with this change. The --log.file option
opens the file once on startup and then keeps writing to the file handle. 
This can become an issue when external log rotation tools are involved, so it's
best not to use them with this option for now.
  • Loading branch information
fjl authored Nov 11, 2022
1 parent 0ad2014 commit 5b4c149
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions internal/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ var (
Usage: "Format logs with JSON",
Category: flags.LoggingCategory,
}
logFileFlag = &cli.StringFlag{
Name: "log.file",
Usage: "Write logs to a file",
Category: flags.LoggingCategory,
}
backtraceAtFlag = &cli.StringFlag{
Name: "log.backtrace",
Usage: "Request a stack trace at a specific logging statement (e.g. \"block.go:271\")",
Expand Down Expand Up @@ -110,6 +115,7 @@ var Flags = []cli.Flag{
verbosityFlag,
vmoduleFlag,
logjsonFlag,
logFileFlag,
backtraceAtFlag,
debugFlag,
pprofFlag,
Expand All @@ -121,7 +127,10 @@ var Flags = []cli.Flag{
traceFlag,
}

var glogger *log.GlogHandler
var (
glogger *log.GlogHandler
logOutputStream log.Handler
)

func init() {
glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
Expand All @@ -132,18 +141,30 @@ func init() {
// 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 {
var ostream log.Handler
output := io.Writer(os.Stderr)
logFile := ctx.String(logFileFlag.Name)
useColor := logFile == "" && os.Getenv("TERM") != "dumb" && (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()))

var logfmt log.Format
if ctx.Bool(logjsonFlag.Name) {
ostream = log.StreamHandler(output, log.JSONFormat())
logfmt = log.JSONFormat()
} else {
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
if usecolor {
logfmt = log.TerminalFormat(useColor)
}

if logFile != "" {
var err error
logOutputStream, err = log.FileHandler(logFile, logfmt)
if err != nil {
return err
}
} else {
output := io.Writer(os.Stderr)
if useColor {
output = colorable.NewColorableStderr()
}
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
logOutputStream = log.StreamHandler(output, logfmt)
}
glogger.SetHandler(ostream)
glogger.SetHandler(logOutputStream)

// logging
verbosity := ctx.Int(verbosityFlag.Name)
Expand Down Expand Up @@ -217,4 +238,7 @@ func StartPProf(address string, withMetrics bool) {
func Exit() {
Handler.StopCPUProfile()
Handler.StopGoTrace()
if closer, ok := logOutputStream.(io.Closer); ok {
closer.Close()
}
}

0 comments on commit 5b4c149

Please sign in to comment.