Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
feat: support log level and log format flags (#591)
Browse files Browse the repository at this point in the history
* feat: support log level and log format flags

* fix: deploy script
  • Loading branch information
rach-id authored Nov 14, 2023
1 parent 5590cb8 commit 063bec5
Show file tree
Hide file tree
Showing 18 changed files with 322 additions and 54 deletions.
59 changes: 59 additions & 0 deletions cmd/blobstream/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package base

import (
"fmt"
"io"
"os"
"strings"

"github.com/cosmos/cosmos-sdk/server"
"github.com/rs/zerolog"
tmconfig "github.com/tendermint/tendermint/config"
tmlog "github.com/tendermint/tendermint/libs/log"

ethcmn "github.com/ethereum/go-ethereum/common"

"github.com/celestiaorg/orchestrator-relayer/evm"
Expand Down Expand Up @@ -75,8 +81,45 @@ const (
FlagCoreRPC = "core.rpc"

FlagStartingNonce = "starting-nonce"

FlagLogLevel = "log.level"
FlagLogFormat = "log.format"
)

func AddLogLevelFlag(cmd *cobra.Command) {
cmd.Flags().String(
FlagLogLevel,
"info",
"The logging level (trace|debug|info|warn|error|fatal|panic)",
)
}

func GetLogFormatFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagLogFormat)
val, err := cmd.Flags().GetString(FlagLogFormat)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddLogFormatFlag(cmd *cobra.Command) {
cmd.Flags().String(
FlagLogFormat,
"plain",
"The logging format (json|plain)",
)
}

func GetLogLevelFlag(cmd *cobra.Command) (string, bool, error) {
changed := cmd.Flags().Changed(FlagLogLevel)
val, err := cmd.Flags().GetString(FlagLogLevel)
if err != nil {
return "", changed, err
}
return val, changed, nil
}

func AddStartingNonceFlag(cmd *cobra.Command) {
cmd.Flags().String(
FlagStartingNonce,
Expand Down Expand Up @@ -278,3 +321,19 @@ func ValidateEVMAddress(addr string) error {
func EnsureConfigPath(configPath string) error {
return os.MkdirAll(configPath, os.ModePerm)
}

// GetLogger creates a new logger and returns
func GetLogger(level string, format string) (tmlog.Logger, error) {
logLvl, err := zerolog.ParseLevel(level)
if err != nil {
return nil, fmt.Errorf("failed to parse log level (%s): %w", level, err)
}
var logWriter io.Writer
if strings.ToLower(format) == tmconfig.LogFormatPlain {
logWriter = zerolog.ConsoleWriter{Out: os.Stderr}
} else {
logWriter = os.Stderr
}

return server.ZeroLogWrapper{Logger: zerolog.New(logWriter).Level(logLvl).With().Timestamp().Logger()}, nil
}
16 changes: 11 additions & 5 deletions cmd/blobstream/bootstrapper/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package bootstrapper

import (
"context"
"os"
"strings"
"time"

"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/base"

p2pcmd "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/keys/p2p"
"github.com/celestiaorg/orchestrator-relayer/helpers"
"github.com/celestiaorg/orchestrator-relayer/p2p"
Expand All @@ -14,7 +15,6 @@ import (
dssync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/spf13/cobra"
tmlog "github.com/tendermint/tendermint/libs/log"
)

func Command() *cobra.Command {
Expand Down Expand Up @@ -48,8 +48,11 @@ func Start() *cobra.Command {
}

// creating the logger
logger := tmlog.NewTMLogger(os.Stdout)
logger.Debug("starting bootstrapper node")
logger, err := base.GetLogger(config.logLevel, config.logFormat)
if err != nil {
return err
}
logger.Info("starting bootstrapper node")

ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
Expand Down Expand Up @@ -144,7 +147,10 @@ func Init() *cobra.Command {
return err
}

logger := tmlog.NewTMLogger(os.Stdout)
logger, err := base.GetLogger(config.logLevel, config.logFormat)
if err != nil {
return err
}

initOptions := store.InitOptions{
NeedDataStore: false,
Expand Down
34 changes: 32 additions & 2 deletions cmd/blobstream/bootstrapper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ func addStartFlags(cmd *cobra.Command) *cobra.Command {
base.AddP2PNicknameFlag(cmd)
base.AddP2PListenAddressFlag(cmd)
base.AddBootstrappersFlag(cmd)
base.AddLogLevelFlag(cmd)
base.AddLogFormatFlag(cmd)
return cmd
}

type StartConfig struct {
home string
p2pListenAddr, p2pNickname string
bootstrappers string
logLevel string
logFormat string
}

func parseStartFlags(cmd *cobra.Command) (StartConfig, error) {
Expand Down Expand Up @@ -52,11 +56,23 @@ func parseStartFlags(cmd *cobra.Command) (StartConfig, error) {
return StartConfig{}, err
}

logLevel, _, err := base.GetLogLevelFlag(cmd)
if err != nil {
return StartConfig{}, err
}

logFormat, _, err := base.GetLogFormatFlag(cmd)
if err != nil {
return StartConfig{}, err
}

return StartConfig{
p2pNickname: p2pNickname,
p2pListenAddr: p2pListenAddress,
home: homeDir,
bootstrappers: bootstrappers,
logFormat: logFormat,
logLevel: logLevel,
}, nil
}

Expand All @@ -66,11 +82,15 @@ func addInitFlags(cmd *cobra.Command) *cobra.Command {
panic(err)
}
cmd.Flags().String(base.FlagHome, homeDir, "The Blobstream bootstrappers home directory")
base.AddLogLevelFlag(cmd)
base.AddLogFormatFlag(cmd)
return cmd
}

type InitConfig struct {
home string
home string
logLevel string
logFormat string
}

func parseInitFlags(cmd *cobra.Command) (InitConfig, error) {
Expand All @@ -85,8 +105,18 @@ func parseInitFlags(cmd *cobra.Command) (InitConfig, error) {
return InitConfig{}, err
}
}
logLevel, _, err := base.GetLogLevelFlag(cmd)
if err != nil {
return InitConfig{}, err
}

logFormat, _, err := base.GetLogFormatFlag(cmd)
if err != nil {
return InitConfig{}, err
}
return InitConfig{
home: homeDir,
home: homeDir,
logFormat: logFormat,
logLevel: logLevel,
}, nil
}
10 changes: 7 additions & 3 deletions cmd/blobstream/deploy/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package deploy

import (
"context"
"os"
"strconv"

"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/base"

evm2 "github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/keys/evm"

"github.com/celestiaorg/orchestrator-relayer/cmd/blobstream/keys"
Expand Down Expand Up @@ -32,7 +33,10 @@ func Command() *cobra.Command {
return err
}

logger := tmlog.NewTMLogger(os.Stdout)
logger, err := base.GetLogger(config.logLevel, config.logFormat)
if err != nil {
return err
}

// checking if the provided home is already initiated
isInit := store.IsInit(logger, config.Home, store.InitOptions{NeedEVMKeyStore: true})
Expand Down Expand Up @@ -109,7 +113,7 @@ func Command() *cobra.Command {
}(s.EVMKeyStore, acc.Address)

evmClient := evm.NewClient(
tmlog.NewTMLogger(os.Stdout),
logger,
nil,
s.EVMKeyStore,
&acc,
Expand Down
16 changes: 16 additions & 0 deletions cmd/blobstream/deploy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func addDeployFlags(cmd *cobra.Command) *cobra.Command {
}
base.AddHomeFlag(cmd, ServiceNameDeployer, homeDir)
base.AddGRPCInsecureFlag(cmd)
base.AddLogLevelFlag(cmd)
base.AddLogFormatFlag(cmd)
return cmd
}

Expand All @@ -41,6 +43,8 @@ type deployConfig struct {
startingNonce string
evmGasLimit uint64
grpcInsecure bool
logLevel string
logFormat string
}

func parseDeployFlags(cmd *cobra.Command) (deployConfig, error) {
Expand Down Expand Up @@ -100,6 +104,16 @@ func parseDeployFlags(cmd *cobra.Command) (deployConfig, error) {
return deployConfig{}, err
}

logLevel, _, err := base.GetLogLevelFlag(cmd)
if err != nil {
return deployConfig{}, err
}

logFormat, _, err := base.GetLogFormatFlag(cmd)
if err != nil {
return deployConfig{}, err
}

return deployConfig{
Config: base.Config{
Home: homeDir,
Expand All @@ -113,5 +127,7 @@ func parseDeployFlags(cmd *cobra.Command) (deployConfig, error) {
startingNonce: startingNonce,
evmGasLimit: evmGasLimit,
grpcInsecure: grpcInsecure,
logFormat: logFormat,
logLevel: logLevel,
}, nil
}
32 changes: 32 additions & 0 deletions cmd/blobstream/keys/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ func keysConfigFlags(cmd *cobra.Command, service string) *cobra.Command {
}
cmd.Flags().String(base.FlagHome, homeDir, "The Blobstream evm keys home directory")
cmd.Flags().String(base.FlagEVMPassphrase, "", "the evm account passphrase (if not specified as a flag, it will be asked interactively)")
base.AddLogLevelFlag(cmd)
base.AddLogFormatFlag(cmd)
return cmd
}

type KeysConfig struct {
*base.Config
logLevel string
logFormat string
}

func parseKeysConfigFlags(cmd *cobra.Command, serviceName string) (KeysConfig, error) {
Expand All @@ -41,11 +45,23 @@ func parseKeysConfigFlags(cmd *cobra.Command, serviceName string) (KeysConfig, e
return KeysConfig{}, err
}

logLevel, _, err := base.GetLogLevelFlag(cmd)
if err != nil {
return KeysConfig{}, err
}

logFormat, _, err := base.GetLogFormatFlag(cmd)
if err != nil {
return KeysConfig{}, err
}

return KeysConfig{
Config: &base.Config{
Home: homeDir,
EVMPassphrase: passphrase,
},
logFormat: logFormat,
logLevel: logLevel,
}, nil
}

Expand All @@ -57,12 +73,16 @@ func keysNewPassphraseConfigFlags(cmd *cobra.Command, service string) *cobra.Com
cmd.Flags().String(base.FlagHome, homeDir, "The Blobstream evm keys home directory")
cmd.Flags().String(base.FlagEVMPassphrase, "", "the evm account passphrase (if not specified as a flag, it will be asked interactively)")
cmd.Flags().String(FlagNewEVMPassphrase, "", "the evm account new passphrase (if not specified as a flag, it will be asked interactively)")
base.AddLogLevelFlag(cmd)
base.AddLogFormatFlag(cmd)
return cmd
}

type KeysNewPassphraseConfig struct {
*base.Config
newPassphrase string
logLevel string
logFormat string
}

func parseKeysNewPassphraseConfigFlags(cmd *cobra.Command, serviceName string) (KeysNewPassphraseConfig, error) {
Expand All @@ -87,8 +107,20 @@ func parseKeysNewPassphraseConfigFlags(cmd *cobra.Command, serviceName string) (
return KeysNewPassphraseConfig{}, err
}

logLevel, _, err := base.GetLogLevelFlag(cmd)
if err != nil {
return KeysNewPassphraseConfig{}, err
}

logFormat, _, err := base.GetLogFormatFlag(cmd)
if err != nil {
return KeysNewPassphraseConfig{}, err
}

return KeysNewPassphraseConfig{
Config: &base.Config{Home: homeDir, EVMPassphrase: passphrase},
newPassphrase: newPassphrase,
logFormat: logFormat,
logLevel: logLevel,
}, nil
}
Loading

0 comments on commit 063bec5

Please sign in to comment.