@ -20,8 +20,9 @@ import (
"fmt"
"fmt"
"io"
"io"
"net/http"
"net/http"
_ "net/http/pprof" // nolint: gosec
_ "net/http/pprof"
"os"
"os"
"path/filepath"
"runtime"
"runtime"
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/internal/flags"
@ -32,6 +33,7 @@ import (
"github.com/mattn/go-colorable"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2"
"gopkg.in/natefinch/lumberjack.v2"
)
)
var Memsize memsizeui . Handler
var Memsize memsizeui . Handler
@ -76,6 +78,34 @@ var (
Usage : "Prepends log messages with call-site location (file and line number)" ,
Usage : "Prepends log messages with call-site location (file and line number)" ,
Category : flags . LoggingCategory ,
Category : flags . LoggingCategory ,
}
}
logRotateFlag = & cli . BoolFlag {
Name : "log.rotate" ,
Usage : "Enables log file rotation" ,
}
logMaxSizeMBsFlag = & cli . IntFlag {
Name : "log.maxsize" ,
Usage : "Maximum size in MBs of a single log file" ,
Value : 100 ,
Category : flags . LoggingCategory ,
}
logMaxBackupsFlag = & cli . IntFlag {
Name : "log.maxbackups" ,
Usage : "Maximum number of log files to retain" ,
Value : 10 ,
Category : flags . LoggingCategory ,
}
logMaxAgeFlag = & cli . IntFlag {
Name : "log.maxage" ,
Usage : "Maximum number of days to retain a log file" ,
Value : 30 ,
Category : flags . LoggingCategory ,
}
logCompressFlag = & cli . BoolFlag {
Name : "log.compress" ,
Usage : "Compress the log files" ,
Value : false ,
Category : flags . LoggingCategory ,
}
pprofFlag = & cli . BoolFlag {
pprofFlag = & cli . BoolFlag {
Name : "pprof" ,
Name : "pprof" ,
Usage : "Enable the pprof HTTP server" ,
Usage : "Enable the pprof HTTP server" ,
@ -120,11 +150,16 @@ var (
var Flags = [ ] cli . Flag {
var Flags = [ ] cli . Flag {
verbosityFlag ,
verbosityFlag ,
vmoduleFlag ,
vmoduleFlag ,
backtraceAtFlag ,
debugFlag ,
logjsonFlag ,
logjsonFlag ,
logFormatFlag ,
logFormatFlag ,
logFileFlag ,
logFileFlag ,
backtraceAtFlag ,
logRotateFlag ,
debugFlag ,
logMaxSizeMBsFlag ,
logMaxBackupsFlag ,
logMaxAgeFlag ,
logCompressFlag ,
pprofFlag ,
pprofFlag ,
pprofAddrFlag ,
pprofAddrFlag ,
pprofPortFlag ,
pprofPortFlag ,
@ -148,44 +183,71 @@ func init() {
// 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 {
logFile := ctx . String ( logFileFlag . Name )
var (
useColor := logFile == "" && os . Getenv ( "TERM" ) != "dumb" && ( isatty . IsTerminal ( os . Stderr . Fd ( ) ) || isatty . IsCygwinTerminal ( os . Stderr . Fd ( ) ) )
logfmt log . Format
output = io . Writer ( os . Stderr )
var logfmt log . Format
logFmtFlag = ctx . String ( logFormatFlag . Name )
switch ctx . String ( logFormatFlag . Name ) {
)
case "json" :
switch {
logfmt = log . JSONFormat ( )
case ctx . Bool ( logjsonFlag . Name ) :
case "logfmt" :
logfmt = log . LogfmtFormat ( )
case "terminal" :
logfmt = log . TerminalFormat ( useColor )
case "" :
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
if ctx . Bool ( logjsonFlag . Name ) {
defer log . Warn ( "The flag '--log.json' is deprecated, please use '--log.format=json' instead" )
defer log . Warn ( "The flag '--log.json' is deprecated, please use '--log.format=json' instead" )
logfmt = log . JSONFormat ( )
logfmt = log . JSONFormat ( )
} else {
case logFmtFlag == "json" :
logfmt = log . TerminalFormat ( useColor )
logfmt = log . JSONFormat ( )
case logFmtFlag == "logfmt" :
logfmt = log . LogfmtFormat ( )
case logFmtFlag == "" , logFmtFlag == "terminal" :
useColor := ( isatty . IsTerminal ( os . Stderr . Fd ( ) ) || isatty . IsCygwinTerminal ( os . Stderr . Fd ( ) ) ) && os . Getenv ( "TERM" ) != "dumb"
if useColor {
output = colorable . NewColorableStderr ( )
}
}
logfmt = log . TerminalFormat ( useColor )
default :
default :
// Unknown log format specified
// Unknown log format specified
return fmt . Errorf ( "unknown log format: %v" , ctx . String ( logFormatFlag . Name ) )
return fmt . Errorf ( "unknown log format: %v" , ctx . String ( logFormatFlag . Name ) )
}
}
var (
if logFile != "" {
stdHandler = log . StreamHandler ( output , logfmt )
var err error
ostream = stdHandler
logOutputStream , err = log . FileHandler ( logFile , logfmt )
logFile = ctx . String ( logFileFlag . Name )
if err != nil {
rotation = ctx . Bool ( logRotateFlag . Name )
return err
)
if len ( logFile ) > 0 {
if err := validateLogLocation ( filepath . Dir ( logFile ) ) ; err != nil {
return fmt . Errorf ( "failed to initiatilize file logger: %v" , err )
}
}
}
context := [ ] interface { } { "rotate" , rotation }
if len ( logFmtFlag ) > 0 {
context = append ( context , "format" , logFmtFlag )
} else {
} else {
output := io . Writer ( os . Stderr )
context = append ( context , "format" , "terminal" )
if useColor {
output = colorable . NewColorableStderr ( )
}
}
logOutputStream = log . StreamHandler ( output , logfmt )
if rotation {
// Lumberjack uses <processname>-lumberjack.log in is.TempDir() if empty.
// so typically /tmp/geth-lumberjack.log on linux
if len ( logFile ) > 0 {
context = append ( context , "location" , logFile )
} else {
context = append ( context , "location" , filepath . Join ( os . TempDir ( ) , "geth-lumberjack.log" ) )
}
ostream = log . MultiHandler ( log . StreamHandler ( & lumberjack . Logger {
Filename : logFile ,
MaxSize : ctx . Int ( logMaxSizeMBsFlag . Name ) ,
MaxBackups : ctx . Int ( logMaxBackupsFlag . Name ) ,
MaxAge : ctx . Int ( logMaxAgeFlag . Name ) ,
Compress : ctx . Bool ( logCompressFlag . Name ) ,
} , logfmt ) , stdHandler )
} else if logFile != "" {
if logOutputStream , err := log . FileHandler ( logFile , logfmt ) ; err != nil {
return err
} else {
ostream = log . MultiHandler ( logOutputStream , stdHandler )
context = append ( context , "location" , logFile )
}
}
glogger . SetHandler ( logOutputStream )
}
glogger . SetHandler ( ostream )
// logging
// logging
verbosity := ctx . Int ( verbosityFlag . Name )
verbosity := ctx . Int ( verbosityFlag . Name )
@ -236,6 +298,9 @@ func Setup(ctx *cli.Context) error {
// It cannot be imported because it will cause a cyclical dependency.
// It cannot be imported because it will cause a cyclical dependency.
StartPProf ( address , ! ctx . IsSet ( "metrics.addr" ) )
StartPProf ( address , ! ctx . IsSet ( "metrics.addr" ) )
}
}
if len ( logFile ) > 0 || rotation {
log . Info ( "Logging configured" , context ... )
}
return nil
return nil
}
}
@ -263,3 +328,17 @@ func Exit() {
closer . Close ( )
closer . Close ( )
}
}
}
}
func validateLogLocation ( path string ) error {
if err := os . MkdirAll ( path , os . ModePerm ) ; err != nil {
return fmt . Errorf ( "error creating the directory: %w" , err )
}
// Check if the path is writable by trying to create a temporary file
tmp := filepath . Join ( path , "tmp" )
if f , err := os . Create ( tmp ) ; err != nil {
return err
} else {
f . Close ( )
}
return os . Remove ( tmp )
}