-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/geth: enable log rotation #26843
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,4 @@ profile.cov | |
/dashboard/assets/package-lock.json | ||
|
||
**/yarn-error.log | ||
logs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,8 +20,9 @@ import ( | |||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
_ "net/http/pprof" // nolint: gosec | ||||||
_ "net/http/pprof" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"runtime" | ||||||
|
||||||
"github.com/ethereum/go-ethereum/internal/flags" | ||||||
|
@@ -32,6 +33,7 @@ import ( | |||||
"github.com/mattn/go-colorable" | ||||||
"github.com/mattn/go-isatty" | ||||||
"github.com/urfave/cli/v2" | ||||||
"gopkg.in/natefinch/lumberjack.v2" | ||||||
) | ||||||
|
||||||
var Memsize memsizeui.Handler | ||||||
|
@@ -76,6 +78,34 @@ var ( | |||||
Usage: "Prepends log messages with call-site location (file and line number)", | ||||||
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{ | ||||||
Name: "pprof", | ||||||
Usage: "Enable the pprof HTTP server", | ||||||
|
@@ -120,11 +150,16 @@ var ( | |||||
var Flags = []cli.Flag{ | ||||||
verbosityFlag, | ||||||
vmoduleFlag, | ||||||
backtraceAtFlag, | ||||||
debugFlag, | ||||||
logjsonFlag, | ||||||
logFormatFlag, | ||||||
logFileFlag, | ||||||
backtraceAtFlag, | ||||||
debugFlag, | ||||||
logRotateFlag, | ||||||
logMaxSizeMBsFlag, | ||||||
logMaxBackupsFlag, | ||||||
logMaxAgeFlag, | ||||||
logCompressFlag, | ||||||
pprofFlag, | ||||||
pprofAddrFlag, | ||||||
pprofPortFlag, | ||||||
|
@@ -148,44 +183,66 @@ 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 { | ||||||
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 | ||||||
switch ctx.String(logFormatFlag.Name) { | ||||||
case "json": | ||||||
var ( | ||||||
logfmt log.Format | ||||||
output = io.Writer(os.Stderr) | ||||||
logFmtFlag = ctx.String(logFormatFlag.Name) | ||||||
) | ||||||
switch { | ||||||
case ctx.Bool(logjsonFlag.Name): | ||||||
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set | ||||||
defer log.Warn("The flag '--log.json' is deprecated, please use '--log.format=json' instead") | ||||||
logfmt = log.JSONFormat() | ||||||
case "logfmt": | ||||||
case logFmtFlag == "json": | ||||||
logfmt = log.JSONFormat() | ||||||
case logFmtFlag == "logfmt": | ||||||
logfmt = log.LogfmtFormat() | ||||||
case "terminal": | ||||||
logfmt = log.TerminalFormat(useColor) | ||||||
case "": | ||||||
// 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") | ||||||
logfmt = log.JSONFormat() | ||||||
} else { | ||||||
logfmt = log.TerminalFormat(useColor) | ||||||
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: | ||||||
// Unknown log format specified | ||||||
return fmt.Errorf("unknown log format: %v", ctx.String(logFormatFlag.Name)) | ||||||
} | ||||||
|
||||||
if logFile != "" { | ||||||
var err error | ||||||
logOutputStream, err = log.FileHandler(logFile, logfmt) | ||||||
if err != nil { | ||||||
return err | ||||||
var ( | ||||||
stdHandler = log.StreamHandler(output, logfmt) | ||||||
ostream = stdHandler | ||||||
logFile = ctx.String(logFileFlag.Name) | ||||||
rotation = ctx.Bool(logRotateFlag.Name) | ||||||
) | ||||||
if len(logFile) > 0 { | ||||||
if err := validateLogLocation(filepath.Dir(logFile)); err != nil { | ||||||
return fmt.Errorf("failed to initiatilize file logger: %v", err) | ||||||
} | ||||||
} else { | ||||||
output := io.Writer(os.Stderr) | ||||||
if useColor { | ||||||
output = colorable.NewColorableStderr() | ||||||
} | ||||||
if rotation { | ||||||
// Lumberjack uses <processname>-lumberjack.log in is.TempDir() if empty. | ||||||
// so typically /tmp/geth-lumberjack.log on linux | ||||||
if len(logFile) > 0 { | ||||||
log.Info("Rotating file logging configured", "location", logFile) | ||||||
} else { | ||||||
log.Info("Rotating file logging configured", "location", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
log.Info("File logging configured", "location", logFile) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
logOutputStream = log.StreamHandler(output, logfmt) | ||||||
} | ||||||
glogger.SetHandler(logOutputStream) | ||||||
glogger.SetHandler(ostream) | ||||||
|
||||||
// logging | ||||||
verbosity := ctx.Int(verbosityFlag.Name) | ||||||
|
@@ -263,3 +320,17 @@ func Exit() { | |||||
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) | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should defer these? In case
format=json
is specified, it's a bit annoying to have a non-json regular log being spat out before the json-logs.@fjl wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your reasoning that it's not good to print a message while logging is not fully configured yet.
I'm also wondering, are these messages really necessary? I think these should be at debug level or just be removed. Geth already prints a ton of INFO messages at startup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not...
Though I generally find it annoying when debugging something that does not work, and it is not possible to see "what does this application think I told it to do". And with rotation, you won't notice that it does (or does not) work until much later, when it fails to rotate.
Like, if you configure everything about the rotation, but omitted
log.rotate
, then it's better UX if users are able to notice immediately. (and now that I say that, I also feel that we should add "rotate","off" on the other message)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, now I do print out the info in case non-default options are used. I'm happy with it now