diff --git a/cmd/cmd.go b/cmd/cmd.go index dace04ab3..02351a2c1 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -261,13 +261,13 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, ) { // To make process faster, we update the password after creating the addresses walletPath := PactusDefaultWalletPath(workingDir) - wallet, err := wallet.Create(walletPath, mnemonic, "", chain) + walletInstance, err := wallet.Create(walletPath, mnemonic, "", chain) if err != nil { return nil, nil, err } for i := 0; i < numValidators; i++ { - addr, err := wallet.DeriveNewAddress(fmt.Sprintf("Validator address %v", i+1)) + addr, err := walletInstance.DeriveNewAddress(fmt.Sprintf("Validator address %v", i+1)) if err != nil { return nil, nil, err } @@ -275,7 +275,7 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, } for i := 0; i < numValidators; i++ { - addr, err := wallet.DeriveNewAddress(fmt.Sprintf("Reward address %v", i+1)) + addr, err := walletInstance.DeriveNewAddress(fmt.Sprintf("Reward address %v", i+1)) if err != nil { return nil, nil, err } @@ -299,7 +299,7 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, return nil, nil, err } case genesis.Localnet: - err = makeLocalGenesis(*wallet).SaveToFile(genPath) + err = makeLocalGenesis(*walletInstance).SaveToFile(genPath) if err != nil { return nil, nil, err } @@ -310,12 +310,12 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string, } } - err = wallet.UpdatePassword("", walletPassword) + err = walletInstance.UpdatePassword("", walletPassword) if err != nil { return nil, nil, err } - err = wallet.Save() + err = walletInstance.Save() if err != nil { return nil, nil, err } @@ -389,11 +389,11 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, } walletPath := PactusDefaultWalletPath(workingDir) - wallet, err := wallet.Open(walletPath, true) + walletInstance, err := wallet.Open(walletPath, true) if err != nil { return nil, nil, err } - addrLabels := wallet.AddressLabels() + addrLabels := walletInstance.AddressLabels() // Create signers if len(addrLabels) < conf.Node.NumValidators { @@ -404,11 +404,11 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, validatorAddrs[i] = addrLabels[i].Address } signers := make([]crypto.Signer, conf.Node.NumValidators) - password, ok := passwordFetcher(wallet) + password, ok := passwordFetcher(walletInstance) if !ok { return nil, nil, fmt.Errorf("aborted") } - prvKeys, err := wallet.PrivateKeys(password, validatorAddrs) + prvKeys, err := walletInstance.PrivateKeys(password, validatorAddrs) if err != nil { return nil, nil, err } @@ -431,17 +431,17 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, } } - node, err := node.NewNode(gen, conf, signers, rewardAddrs) + nodeInstance, err := node.NewNode(gen, conf, signers, rewardAddrs) if err != nil { return nil, nil, err } - err = node.Start() + err = nodeInstance.Start() if err != nil { return nil, nil, err } - return node, wallet, nil + return nodeInstance, walletInstance, nil } // makeLocalGenesis makes genesis file for the local network. diff --git a/go.mod b/go.mod index 01dbd14ad..21f218dbc 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( golang.org/x/crypto v0.7.0 google.golang.org/grpc v1.53.0 google.golang.org/protobuf v1.30.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( diff --git a/go.sum b/go.sum index 46a1578c7..ec60c951a 100644 --- a/go.sum +++ b/go.sum @@ -702,6 +702,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/util/logger/config.go b/util/logger/config.go index 2b89273dc..8e25ba394 100644 --- a/util/logger/config.go +++ b/util/logger/config.go @@ -1,5 +1,10 @@ package logger +const ( + LogDirectory = "logs" + LogFilename = "pactus.log" +) + type Config struct { Colorful bool `toml:"colorful"` Levels map[string]string `toml:"levels"` diff --git a/util/logger/logger.go b/util/logger/logger.go index fe11771b5..8d02fca9f 100644 --- a/util/logger/logger.go +++ b/util/logger/logger.go @@ -3,7 +3,11 @@ package logger import ( "encoding/hex" "fmt" + "io" "os" + "path/filepath" + + "gopkg.in/natefinch/lumberjack.v2" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -89,14 +93,28 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event { } func NewSubLogger(name string, obj fmt.Stringer) *SubLogger { + var writers []io.Writer + maxLogSize := 100 + + // console writer + writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr}) + logFilename := filepath.Join("./", LogFilename) + + fl := &lumberjack.Logger{ + Filename: logFilename, + MaxSize: maxLogSize, + } + writers = append(writers, fl) + + mw := io.MultiWriter(writers...) sl := &SubLogger{ - logger: zerolog.New(os.Stderr).With().Timestamp().Logger(), + logger: zerolog.New(mw).With().Timestamp().Logger(), name: name, obj: obj, } if getLoggersInst().config.Colorful { - sl.logger = sl.logger.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + sl.logger = sl.logger.Output(mw) } lvlStr := getLoggersInst().config.Levels[name] @@ -109,6 +127,13 @@ func NewSubLogger(name string, obj fmt.Stringer) *SubLogger { sl.logger.Level(lvl) } + sl.logger.Info(). + Bool("fileLogging", true). + Bool("jsonLogOutput", true). + Str("fileName", logFilename). + Int("maxSizeMB", maxLogSize). + Msg("logging configured") + getLoggersInst().subs[name] = sl return sl }