mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
log: format output
This commit is contained in:
parent
c10ac4f48f
commit
4a397df329
1 changed files with 24 additions and 5 deletions
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/beacon/blsync"
|
"github.com/ethereum/go-ethereum/beacon/blsync"
|
||||||
|
|
@ -47,6 +48,11 @@ var (
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
Category: flags.LoggingCategory,
|
Category: flags.LoggingCategory,
|
||||||
}
|
}
|
||||||
|
logFmtFlag = &cli.StringFlag{
|
||||||
|
Name: "log.format",
|
||||||
|
Usage: "Log format to use (json|logfmt|terminal)",
|
||||||
|
Category: flags.LoggingCategory,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -68,6 +74,7 @@ func main() {
|
||||||
utils.BlsyncJWTSecretFlag,
|
utils.BlsyncJWTSecretFlag,
|
||||||
verbosityFlag,
|
verbosityFlag,
|
||||||
vmoduleFlag,
|
vmoduleFlag,
|
||||||
|
logFmtFlag,
|
||||||
}
|
}
|
||||||
app.Action = sync
|
app.Action = sync
|
||||||
|
|
||||||
|
|
@ -78,13 +85,25 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func sync(ctx *cli.Context) error {
|
func sync(ctx *cli.Context) error {
|
||||||
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
|
||||||
output := io.Writer(os.Stderr)
|
output := io.Writer(os.Stderr)
|
||||||
if usecolor {
|
|
||||||
output = colorable.NewColorable(os.Stderr)
|
|
||||||
}
|
|
||||||
verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name))
|
verbosity := log.FromLegacyLevel(ctx.Int(verbosityFlag.Name))
|
||||||
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, verbosity, usecolor)))
|
|
||||||
|
var handler slog.Handler
|
||||||
|
switch ctx.String(logFmtFlag.Name) {
|
||||||
|
case "json":
|
||||||
|
handler = log.JSONHandlerWithLevel(output, verbosity)
|
||||||
|
case "logfmt":
|
||||||
|
handler = log.LogfmtHandlerWithLevel(output, verbosity)
|
||||||
|
case "", "terminal":
|
||||||
|
usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
|
||||||
|
if usecolor {
|
||||||
|
output = colorable.NewColorable(os.Stderr)
|
||||||
|
}
|
||||||
|
handler = log.NewTerminalHandlerWithLevel(output, verbosity, usecolor)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown log format: %v", logFmtFlag.Name)
|
||||||
|
}
|
||||||
|
log.SetDefault(log.NewLogger(handler))
|
||||||
|
|
||||||
// set up blsync
|
// set up blsync
|
||||||
client := blsync.NewClient(ctx)
|
client := blsync.NewClient(ctx)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue