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

Change log-level cli opt to take a string #2279

Merged
merged 2 commits into from
May 7, 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
15 changes: 9 additions & 6 deletions cmd/daserver/daserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"syscall"
"time"

"golang.org/x/exp/slog"

koanfjson "github.com/knadh/koanf/parsers/json"
flag "github.com/spf13/pflag"

Expand Down Expand Up @@ -46,7 +44,7 @@ type DAServerConfig struct {
DataAvailability das.DataAvailabilityConfig `koanf:"data-availability"`

Conf genericconf.ConfConfig `koanf:"conf"`
LogLevel int `koanf:"log-level"`
LogLevel string `koanf:"log-level"`
LogType string `koanf:"log-type"`

Metrics bool `koanf:"metrics"`
Expand All @@ -66,7 +64,7 @@ var DefaultDAServerConfig = DAServerConfig{
RESTServerTimeouts: genericconf.HTTPServerTimeoutConfigDefault,
DataAvailability: das.DefaultDataAvailabilityConfig,
Conf: genericconf.ConfConfigDefault,
LogLevel: int(log.LvlInfo),
LogLevel: "INFO",
LogType: "plaintext",
Metrics: false,
MetricsServer: genericconf.MetricsServerConfigDefault,
Expand Down Expand Up @@ -103,7 +101,7 @@ func parseDAServer(args []string) (*DAServerConfig, error) {
f.Bool("pprof", DefaultDAServerConfig.PProf, "enable pprof")
genericconf.PProfAddOptions("pprof-cfg", f)

f.Int("log-level", int(log.LvlInfo), "log level; 1: ERROR, 2: WARN, 3: INFO, 4: DEBUG, 5: TRACE")
f.String("log-level", DefaultDAServerConfig.LogLevel, "log level, valid values are CRIT, ERROR, WARN, INFO, DEBUG, TRACE")
f.String("log-type", DefaultDAServerConfig.LogType, "log type (plaintext or json)")

das.DataAvailabilityConfigAddDaserverOptions("data-availability", f)
Expand Down Expand Up @@ -185,13 +183,18 @@ func startup() error {
confighelpers.PrintErrorAndExit(errors.New("please specify at least one of --enable-rest or --enable-rpc"), printSampleUsage)
}

logLevel, err := genericconf.ToSlogLevel(serverConfig.LogLevel)
if err != nil {
confighelpers.PrintErrorAndExit(err, printSampleUsage)
}

handler, err := genericconf.HandlerFromLogType(serverConfig.LogType, io.Writer(os.Stderr))
if err != nil {
flag.Usage()
return fmt.Errorf("error parsing log type when creating handler: %w", err)
}
glogger := log.NewGlogHandler(handler)
glogger.Verbosity(slog.Level(serverConfig.LogLevel))
glogger.Verbosity(logLevel)
log.SetDefault(log.NewLogger(glogger))

if err := startMetrics(serverConfig); err != nil {
Expand Down
11 changes: 8 additions & 3 deletions cmd/genericconf/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"

"github.com/ethereum/go-ethereum/log"
"golang.org/x/exp/slog"
"gopkg.in/natefinch/lumberjack.v2"
)

Expand Down Expand Up @@ -90,7 +89,7 @@ func (l *fileLoggerFactory) close() error {
}

// initLog is not threadsafe
func InitLog(logType string, logLevel slog.Level, fileLoggingConfig *FileLoggingConfig, pathResolver func(string) string) error {
func InitLog(logType string, logLevel string, fileLoggingConfig *FileLoggingConfig, pathResolver func(string) string) error {
var glogger *log.GlogHandler
// always close previous instance of file logger
if err := globalFileLoggerFactory.close(); err != nil {
Expand All @@ -111,8 +110,14 @@ func InitLog(logType string, logLevel slog.Level, fileLoggingConfig *FileLogging
flag.Usage()
return fmt.Errorf("error parsing log type when creating handler: %w", err)
}
slogLevel, err := ToSlogLevel(logLevel)
if err != nil {
flag.Usage()
return fmt.Errorf("error parsing log level: %w", err)
}

glogger = log.NewGlogHandler(handler)
glogger.Verbosity(logLevel)
glogger.Verbosity(slogLevel)
log.SetDefault(log.NewLogger(glogger))
return nil
}
38 changes: 38 additions & 0 deletions cmd/genericconf/loglevel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package genericconf

import (
"errors"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/log"
"golang.org/x/exp/slog"
)

func ToSlogLevel(str string) (slog.Level, error) {
switch strings.ToLower(str) {
case "trace":
return log.LevelTrace, nil
case "debug":
return log.LevelDebug, nil
case "info":
return log.LevelInfo, nil
case "warn":
return log.LevelWarn, nil
case "error":
return log.LevelError, nil
case "crit":
return log.LevelCrit, nil
default:
legacyLevel, err := strconv.Atoi(str)
if err != nil {
// Leave legacy geth numeric log levels undocumented, but if anyone happens
// to be using them, it will work.
return log.LevelTrace, errors.New("invalid log-level")
}
return log.FromLegacyLevel(legacyLevel), nil
}
}
8 changes: 4 additions & 4 deletions cmd/nitro-val/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"fmt"

"reflect"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
Expand All @@ -20,7 +20,7 @@ import (
type ValidationNodeConfig struct {
Conf genericconf.ConfConfig `koanf:"conf" reload:"hot"`
Validation valnode.Config `koanf:"validation" reload:"hot"`
LogLevel int `koanf:"log-level" reload:"hot"`
LogLevel string `koanf:"log-level" reload:"hot"`
LogType string `koanf:"log-type" reload:"hot"`
FileLogging genericconf.FileLoggingConfig `koanf:"file-logging" reload:"hot"`
Persistent conf.PersistentConfig `koanf:"persistent"`
Expand Down Expand Up @@ -61,7 +61,7 @@ var IPCConfigDefault = genericconf.IPCConfig{

var ValidationNodeConfigDefault = ValidationNodeConfig{
Conf: genericconf.ConfConfigDefault,
LogLevel: int(log.LvlInfo),
LogLevel: "INFO",
LogType: "plaintext",
Persistent: conf.PersistentConfigDefault,
HTTP: HTTPConfigDefault,
Expand All @@ -79,7 +79,7 @@ var ValidationNodeConfigDefault = ValidationNodeConfig{
func ValidationNodeConfigAddOptions(f *flag.FlagSet) {
genericconf.ConfConfigAddOptions("conf", f)
valnode.ValidationConfigAddOptions("validation", f)
f.Int("log-level", ValidationNodeConfigDefault.LogLevel, "log level")
f.String("log-level", ValidationNodeConfigDefault.LogLevel, "log level, valid values are CRIT, ERROR, WARN, INFO, DEBUG, TRACE")
f.String("log-type", ValidationNodeConfigDefault.LogType, "log type (plaintext or json)")
genericconf.FileLoggingConfigAddOptions("file-logging", f)
conf.PersistentConfigAddOptions("persistent", f)
Expand Down
5 changes: 2 additions & 3 deletions cmd/nitro-val/nitro_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/offchainlabs/nitro/cmd/util/confighelpers"
_ "github.com/offchainlabs/nitro/execution/nodeInterface"
"github.com/offchainlabs/nitro/validator/valnode"
"golang.org/x/exp/slog"
)

func printSampleUsage(name string) {
Expand Down Expand Up @@ -90,7 +89,7 @@ func mainImpl() int {
}
}

err = genericconf.InitLog(nodeConfig.LogType, slog.Level(nodeConfig.LogLevel), &nodeConfig.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
err = genericconf.InitLog(nodeConfig.LogType, nodeConfig.LogLevel, &nodeConfig.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing logging: %v\n", err)
return 1
Expand All @@ -109,7 +108,7 @@ func mainImpl() int {
liveNodeConfig := genericconf.NewLiveConfig[*ValidationNodeConfig](args, nodeConfig, ParseNode)
liveNodeConfig.SetOnReloadHook(func(oldCfg *ValidationNodeConfig, newCfg *ValidationNodeConfig) error {

return genericconf.InitLog(newCfg.LogType, slog.Level(newCfg.LogLevel), &newCfg.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
return genericconf.InitLog(newCfg.LogType, newCfg.LogLevel, &newCfg.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
})

valnode.EnsureValidationExposedViaAuthRPC(&stackConf)
Expand Down
11 changes: 5 additions & 6 deletions cmd/nitro/nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import (
"github.com/offchainlabs/nitro/util/signature"
"github.com/offchainlabs/nitro/validator/server_common"
"github.com/offchainlabs/nitro/validator/valnode"
"golang.org/x/exp/slog"
)

func printSampleUsage(name string) {
Expand Down Expand Up @@ -208,7 +207,7 @@ func mainImpl() int {
}
stackConf.JWTSecret = filename
}
err = genericconf.InitLog(nodeConfig.LogType, slog.Level(nodeConfig.LogLevel), &nodeConfig.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
err = genericconf.InitLog(nodeConfig.LogType, nodeConfig.LogLevel, &nodeConfig.FileLogging, pathResolver(nodeConfig.Persistent.LogDir))
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing logging: %v\n", err)
return 1
Expand Down Expand Up @@ -600,7 +599,7 @@ func mainImpl() int {
}

liveNodeConfig.SetOnReloadHook(func(oldCfg *NodeConfig, newCfg *NodeConfig) error {
if err := genericconf.InitLog(newCfg.LogType, slog.Level(newCfg.LogLevel), &newCfg.FileLogging, pathResolver(nodeConfig.Persistent.LogDir)); err != nil {
if err := genericconf.InitLog(newCfg.LogType, newCfg.LogLevel, &newCfg.FileLogging, pathResolver(nodeConfig.Persistent.LogDir)); err != nil {
return fmt.Errorf("failed to re-init logging: %w", err)
}
return currentNode.OnConfigReload(&oldCfg.Node, &newCfg.Node)
Expand Down Expand Up @@ -691,7 +690,7 @@ type NodeConfig struct {
Validation valnode.Config `koanf:"validation" reload:"hot"`
ParentChain conf.ParentChainConfig `koanf:"parent-chain" reload:"hot"`
Chain conf.L2Config `koanf:"chain"`
LogLevel int `koanf:"log-level" reload:"hot"`
LogLevel string `koanf:"log-level" reload:"hot"`
LogType string `koanf:"log-type" reload:"hot"`
FileLogging genericconf.FileLoggingConfig `koanf:"file-logging" reload:"hot"`
Persistent conf.PersistentConfig `koanf:"persistent"`
Expand All @@ -717,7 +716,7 @@ var NodeConfigDefault = NodeConfig{
Validation: valnode.DefaultValidationConfig,
ParentChain: conf.L1ConfigDefault,
Chain: conf.L2ConfigDefault,
LogLevel: int(log.LvlInfo),
LogLevel: "INFO",
LogType: "plaintext",
FileLogging: genericconf.DefaultFileLoggingConfig,
Persistent: conf.PersistentConfigDefault,
Expand All @@ -743,7 +742,7 @@ func NodeConfigAddOptions(f *flag.FlagSet) {
valnode.ValidationConfigAddOptions("validation", f)
conf.L1ConfigAddOptions("parent-chain", f)
conf.L2ConfigAddOptions("chain", f)
f.Int("log-level", NodeConfigDefault.LogLevel, "log level")
f.String("log-level", NodeConfigDefault.LogLevel, "log level, valid values are CRIT, ERROR, WARN, INFO, DEBUG, TRACE")
f.String("log-type", NodeConfigDefault.LogType, "log type (plaintext or json)")
genericconf.FileLoggingConfigAddOptions("file-logging", f)
conf.PersistentConfigAddOptions("persistent", f)
Expand Down
7 changes: 5 additions & 2 deletions cmd/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/cmd/util/confighelpers"
"github.com/offchainlabs/nitro/relay"
"golang.org/x/exp/slog"
)

func main() {
Expand Down Expand Up @@ -69,8 +68,12 @@ func startup() error {
flag.Usage()
return fmt.Errorf("error parsing log type when creating handler: %w", err)
}
logLevel, err := genericconf.ToSlogLevel(relayConfig.LogLevel)
if err != nil {
confighelpers.PrintErrorAndExit(err, printSampleUsage)
}
glogger := log.NewGlogHandler(handler)
glogger.Verbosity(slog.Level(relayConfig.LogLevel))
glogger.Verbosity(logLevel)
log.SetDefault(log.NewLogger(glogger))

vcsRevision, _, vcsTime := confighelpers.GetVersion()
Expand Down
8 changes: 3 additions & 5 deletions relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

flag "github.com/spf13/pflag"

"github.com/ethereum/go-ethereum/log"

"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/broadcastclient"
"github.com/offchainlabs/nitro/broadcastclients"
Expand Down Expand Up @@ -120,7 +118,7 @@ func (r *Relay) StopAndWait() {
type Config struct {
Conf genericconf.ConfConfig `koanf:"conf"`
Chain L2Config `koanf:"chain"`
LogLevel int `koanf:"log-level"`
LogLevel string `koanf:"log-level"`
LogType string `koanf:"log-type"`
Metrics bool `koanf:"metrics"`
MetricsServer genericconf.MetricsServerConfig `koanf:"metrics-server"`
Expand All @@ -133,7 +131,7 @@ type Config struct {
var ConfigDefault = Config{
Conf: genericconf.ConfConfigDefault,
Chain: L2ConfigDefault,
LogLevel: int(log.LvlInfo),
LogLevel: "INFO",
LogType: "plaintext",
Metrics: false,
MetricsServer: genericconf.MetricsServerConfigDefault,
Expand All @@ -146,7 +144,7 @@ var ConfigDefault = Config{
func ConfigAddOptions(f *flag.FlagSet) {
genericconf.ConfConfigAddOptions("conf", f)
L2ConfigAddOptions("chain", f)
f.Int("log-level", ConfigDefault.LogLevel, "log level")
f.String("log-level", ConfigDefault.LogLevel, "log level, valid values are CRIT, ERROR, WARN, INFO, DEBUG, TRACE")
f.String("log-type", ConfigDefault.LogType, "log type")
f.Bool("metrics", ConfigDefault.Metrics, "enable metrics")
genericconf.MetricsServerAddOptions("metrics-server", f)
Expand Down
Loading