Skip to content

Commit

Permalink
add color flag and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
tinoh9 committed May 3, 2023
1 parent 6baadd0 commit f1eaa06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ const (
dbPathF = "db-path"
networkF = "network"
pprofF = "pprof"
colorF = "color"

defaultConfig = ""
defaultRPCPort = uint16(6060)
defaultDBPath = ""
defaultPprof = false
defaultColor = false

configFlagUsage = "The yaml configuration file."
logLevelFlagUsage = "Options: debug, info, warn, error."
Expand All @@ -44,6 +46,7 @@ const (
dbPathUsage = "Location of the database files."
networkUsage = "Options: mainnet, goerli, goerli2, integration."
pprofUsage = "Enables the pprof server and listens on port 9080."
colorUsage = "Enables colorized outputs (ANSI Escape Codes)."
)

var Version string
Expand Down Expand Up @@ -76,7 +79,7 @@ func main() {
}
}

// NewCmd returns a command that can be exected with any of the Cobra Execute* functions.
// NewCmd returns a command that can be executed with any of the Cobra Execute* functions.
// The RunE field is set to the user-provided run function, allowing for robust testing setups.
//
// 1. NewCmd is called with a non-nil config and a run function.
Expand Down Expand Up @@ -125,6 +128,7 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
junoCmd.Flags().String(dbPathF, defaultDBPath, dbPathUsage)
junoCmd.Flags().Var(&defaultNetwork, networkF, networkUsage)
junoCmd.Flags().Bool(pprofF, defaultPprof, pprofUsage)
junoCmd.Flags().Bool(colorF, defaultColor, colorUsage)

return junoCmd
}
5 changes: 3 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
DatabasePath string `mapstructure:"db-path"`
Network utils.Network `mapstructure:"network"`
Pprof bool `mapstructure:"pprof"`
Color bool `mapstructure:"color"`
}

type Node struct {
Expand All @@ -52,7 +53,7 @@ func New(cfg *Config) (*Node, error) {
}
cfg.DatabasePath = filepath.Join(dirPrefix, cfg.Network.String())
}
log, err := utils.NewZapLogger(cfg.LogLevel)
log, err := utils.NewZapLogger(cfg.LogLevel, cfg.Color)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func makeHTTP(port uint16, rpcHandler *rpc.Handler, log utils.SimpleLogger) *jso
func (n *Node) Run(ctx context.Context) {
n.log.Infow("Starting Juno...", "config", fmt.Sprintf("%+v", *n.cfg))

dbLog, err := utils.NewZapLogger(utils.ERROR)
dbLog, err := utils.NewZapLogger(utils.ERROR, false)
if err != nil {
n.log.Errorw("Error creating DB logger", "err", err)
return
Expand Down
8 changes: 6 additions & 2 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ func NewNopZapLogger() *ZapLogger {
return &ZapLogger{zap.NewNop().Sugar()}
}

func NewZapLogger(logLevel LogLevel) (*ZapLogger, error) {
func NewZapLogger(logLevel LogLevel, color bool) (*ZapLogger, error) {
config := zap.NewProductionConfig()
config.Encoding = "console"
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
if color {
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else {
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
}
config.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Local().Format("15:04:05.000 02/01/2006 -07:00"))
}
Expand Down

0 comments on commit f1eaa06

Please sign in to comment.