Skip to content

Commit

Permalink
Merge pull request #198 from r4f4ss/log_format
Browse files Browse the repository at this point in the history
Add flag to format logs
  • Loading branch information
r4f4ss authored Oct 16, 2024
2 parents e5b5bed + 3384361 commit b8a8f39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
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

0 comments on commit b8a8f39

Please sign in to comment.