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

feat: write logs into file #673

Merged
merged 6 commits into from
Sep 12, 2023
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
26 changes: 13 additions & 13 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,21 @@ 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
}
validatorAddrs = append(validatorAddrs, addr)
}

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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions util/logger/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package logger

const (
LogDirectory = "logs"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need it anymore.

LogFilename = "pactus.log"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this from config.go to logger.go
It is not something to be configured.

)

type Config struct {
Colorful bool `toml:"colorful"`
Levels map[string]string `toml:"levels"`
Expand Down
29 changes: 27 additions & 2 deletions util/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand All @@ -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
}
Expand Down
Loading