Skip to content
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

Add flag to format logs #198

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions cmd/shisui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/ethereum/go-ethereum/portalnetwork/web3"
"github.com/ethereum/go-ethereum/rpc"
"github.com/mattn/go-isatty"
_ "github.com/mattn/go-sqlite3"
"github.com/protolambda/zrnt/eth2/configs"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -79,6 +80,7 @@ var (
utils.PortalDataDirFlag,
utils.PortalDataCapacityFlag,
utils.PortalLogLevelFlag,
utils.PortalLogFormatFlag,
}
metricsFlags = []cli.Flag{
utils.MetricsEnabledFlag,
Expand Down Expand Up @@ -111,7 +113,10 @@ func main() {
}

func shisui(ctx *cli.Context) error {
setDefaultLogger(ctx.Int(utils.PortalLogLevelFlag.Name))
err := setDefaultLogger(ctx.Int(utils.PortalLogLevelFlag.Name), ctx.String(utils.PortalLogFormatFlag.Name))
if err != nil {
return err
}

// Start metrics export if enabled
utils.SetupMetrics(ctx)
Expand Down Expand Up @@ -139,12 +144,26 @@ func shisui(ctx *cli.Context) error {
return startPortalRpcServer(*config, conn, config.RpcAddr, clientChan)
}

func setDefaultLogger(logLevel int) {
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, true))
func setDefaultLogger(logLevel int, logFormat string) error {
var glogger *log.GlogHandler
switch {
case logFormat == "json":
glogger = log.NewGlogHandler(log.JSONHandler(os.Stderr))
case logFormat == "logfmt":
glogger = log.NewGlogHandler(log.LogfmtHandler(os.Stderr))
case logFormat == "", logFormat == "terminal":
useColor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb"
glogger = log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, useColor))
default:
// Unknown log format specified
return fmt.Errorf("unknown log format: %v", logFormat)
}
slogVerbosity := log.FromLegacyLevel(logLevel)
glogger.Verbosity(slogVerbosity)
defaultLogger := log.NewLogger(glogger)
log.SetDefault(defaultLogger)

return nil
}

func handlerInterrupt(clientChan <-chan *Client) {
Expand Down
6 changes: 6 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,12 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Category: flags.PortalNetworkCategory,
}

PortalLogFormatFlag = &cli.StringFlag{
Name: "logformat",
Usage: "Log format to use (json|logfmt|terminal)",
Category: flags.PortalNetworkCategory,
}

PortalPrivateKeyFlag = &cli.StringFlag{
Name: "private.key",
Usage: "Private key of p2p node, hex format without 0x prifix",
Expand Down