-
Notifications
You must be signed in to change notification settings - Fork 20.4k
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
internal/debug: add switch to format logs with json #22207
Conversation
It doesn't need to be done in On a normal run, this seems to work fine (as in, nothing in the normal case tries to log anything before we have performed Setup):
I'll test it a bit more to see if I can find any place where geth tries to log before we've called |
Oh, yeah there's one more thing aswell: there's this |
I think you can combine the two, and make it even better than it is on
diff --git a/internal/debug/flags.go b/internal/debug/flags.go
index a39a4a3194..92a46d3e8e 100644
--- a/internal/debug/flags.go
+++ b/internal/debug/flags.go
@@ -130,9 +130,16 @@ var DeprecatedFlags = []cli.Flag{
var glogger *log.GlogHandler
+func init() {
+ glogger = log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
+ glogger.Verbosity(log.LvlInfo)
+ log.Root().SetHandler(glogger)
+}
+
// 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 {
+ log.Info("setting up logging")
var ostream log.Handler
output := io.Writer(os.Stderr)
if ctx.GlobalBool(logjsonFlag.Name) {
@@ -144,8 +151,7 @@ func Setup(ctx *cli.Context) error {
}
ostream = log.StreamHandler(output, log.TerminalFormat(usecolor))
}
- glogger := log.NewGlogHandler(ostream)
-
+ glogger.SetHandler(ostream)
// logging
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name))) Running this, which contains an extra logging before we've actually set up the desired logging, yields:
So I think this diff minus the extra log line will make it both 'safe' and functional |
Aha this way |
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.
LGTM. I think this is a really nice feature, and on the upside, it doesn't actually add a lot of complexity or new code.
The only thing I'm a bit unsure about is whether logjson
flag is the best name for it.
Another option I was thinking about would be |
I think maybe I'd prefer just |
@s1na as a follow-up, can you move other flags into the |
adds a flag --log.json which if enabled makes the client format logs with JSON.
adds a flag --log.json which if enabled makes the client format logs with JSON.
adds a flag --log.json which if enabled makes the client format logs with JSON.
This PR adds a switch
--logjson
which if enabled makes the client format logs with JSON.Something I'm not clear about is if setting
glogger
needs to be done ininit
. I had to move it toSetup
to have access to the flags. Could this have an unintended consequence?Fixes #21926