feat: set zap core when using WithLogger

This commit is contained in:
Richard Ramos 2023-02-01 19:35:31 -04:00 committed by RichΛrd
parent 6169a44c24
commit 61cba076bb
7 changed files with 40 additions and 38 deletions

View File

@ -3,12 +3,10 @@ package main
import (
"os"
logging "github.com/ipfs/go-log/v2"
cli "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"github.com/waku-org/go-waku/waku"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/utils"
)
var options waku.Options
@ -106,14 +104,6 @@ func main() {
Before: altsrc.InitInputSourceWithContext(cliFlags, altsrc.NewTomlSourceFromFlagFunc("config-file")),
Flags: cliFlags,
Action: func(c *cli.Context) error {
utils.InitLogger(options.LogEncoding, options.LogOutput)
lvl, err := logging.LevelFromString(options.LogLevel)
if err != nil {
return err
}
logging.SetAllLoggers(lvl)
waku.Execute(options)
return nil
},

View File

@ -12,33 +12,26 @@ import (
"time"
"github.com/ethereum/go-ethereum/crypto"
logging "github.com/ipfs/go-log/v2"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
)
var log = logging.Logger("basic2")
var log = utils.Logger().Named("basic2")
func main() {
lvl, err := logging.LevelFromString("info")
if err != nil {
panic(err)
}
logging.SetAllLoggers(lvl)
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:0"))
hostAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
key, err := randomHex(32)
if err != nil {
log.Error("Could not generate random key")
log.Error("Could not generate random key", zap.Error(err))
return
}
prvKey, err := crypto.HexToECDSA(key)
if err != nil {
log.Error(err)
log.Error("Could not convert hex into ecdsa key", zap.Error(err))
return
}
@ -51,12 +44,12 @@ func main() {
node.WithWakuRelay(),
)
if err != nil {
log.Error(err)
log.Error("Error creating wakunode", zap.Error(err))
return
}
if err := wakuNode.Start(ctx); err != nil {
log.Error(err)
log.Error("Error starting wakunode", zap.Error(err))
return
}
@ -94,7 +87,7 @@ func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) {
payload, err := p.Encode(version)
if err != nil {
log.Error("Error encoding the payload: ", err)
log.Error("Error encoding the payload", zap.Error(err))
return
}
@ -107,7 +100,7 @@ func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) {
_, err = wakuNode.Relay().Publish(ctx, msg)
if err != nil {
log.Error("Error sending a message: ", err)
log.Error("Error sending a message", zap.Error(err))
}
}
@ -121,7 +114,7 @@ func writeLoop(ctx context.Context, wakuNode *node.WakuNode) {
func readLoop(ctx context.Context, wakuNode *node.WakuNode) {
sub, err := wakuNode.Relay().Subscribe(ctx)
if err != nil {
log.Error("Could not subscribe: ", err)
log.Error("Could not subscribe", zap.Error(err))
return
}
@ -132,6 +125,6 @@ func readLoop(ctx context.Context, wakuNode *node.WakuNode) {
return
}
log.Info("Received msg, ", string(payload.Data))
log.Info("Received msg, ", zap.String("data", string(payload.Data)))
}
}

View File

@ -28,12 +28,6 @@ var pubSubTopic = protocol.DefaultPubsubTopic()
const contentTopic = "test"
func main() {
lvl, err := logging.LevelFromString("info")
if err != nil {
panic(err)
}
logging.SetAllLoggers(lvl)
hostAddr1, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:60000")
hostAddr2, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:60001")

View File

@ -23,6 +23,7 @@ import (
dssql "github.com/ipfs/go-ds-sql"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/config"
@ -79,8 +80,6 @@ const dialTimeout = 7 * time.Second
// Execute starts a go-waku node with settings determined by the Options parameter
func Execute(options Options) {
utils.Logger().Info("Version details ", zap.String("version", node.Version), zap.String("commit", node.GitCommit))
if options.GenerateKey {
if err := writePrivateKeyToFile(options.KeyFile, []byte(options.KeyPasswd), options.Overwrite); err != nil {
failOnErr(err, "nodekey error")
@ -115,8 +114,14 @@ func Execute(options Options) {
wmetrics.RecordVersion(ctx, node.Version, node.GitCommit)
}
lvl, err := zapcore.ParseLevel(options.LogLevel)
if err != nil {
failOnErr(err, "log level error")
}
nodeOpts := []node.WakuNodeOption{
node.WithLogger(logger),
node.WithLogLevel(lvl),
node.WithPrivateKey(prvKey),
node.WithHostAddress(hostAddr),
node.WithKeepAlive(options.KeepAlive),
@ -255,6 +260,8 @@ func Execute(options Options) {
wakuNode, err := node.New(nodeOpts...)
utils.Logger().Info("Version details ", zap.String("version", node.Version), zap.String("commit", node.GitCommit))
failOnErr(err, "Wakunode")
addPeers(wakuNode, options.Store.Nodes, string(store.StoreID_v20beta4))

View File

@ -112,7 +112,6 @@ func defaultStoreFactory(w *WakuNode) store.Store {
// New is used to instantiate a WakuNode using a set of WakuNodeOptions
func New(opts ...WakuNodeOption) (*WakuNode, error) {
params := new(WakuNodeParameters)
params.libP2POpts = DefaultLibP2POptions
opts = append(DefaultWakuNodeOptions, opts...)
@ -123,6 +122,10 @@ func New(opts ...WakuNodeOption) (*WakuNode, error) {
}
}
if params.logger == nil {
params.logger = utils.Logger()
}
if params.privKey == nil {
prvKey, err := crypto.GenerateKey()
if err != nil {

View File

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/p2p/enode"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/config"
@ -29,6 +30,7 @@ import (
"github.com/waku-org/go-waku/waku/v2/timesource"
"github.com/waku-org/go-waku/waku/v2/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Default userAgent
@ -111,7 +113,6 @@ type WakuNodeOption func(*WakuNodeParameters) error
// Default options used in the libp2p node
var DefaultWakuNodeOptions = []WakuNodeOption{
WithDiscoverParams(150),
WithLogger(utils.Logger()),
}
// MultiAddresses return the list of multiaddresses configured in the node
@ -138,6 +139,15 @@ func (w WakuNodeParameters) AddressFactory() basichost.AddrsFactory {
func WithLogger(l *zap.Logger) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.logger = l
logging.SetPrimaryCore(l.Core())
return nil
}
}
// WithLogLevel is a WakuNodeOption that sets the log level for go-waku
func WithLogLevel(lvl zapcore.Level) WakuNodeOption {
return func(params *WakuNodeParameters) error {
logging.SetAllLoggers(logging.LogLevel(lvl))
return nil
}
}

View File

@ -44,6 +44,9 @@ func InitLogger(encoding string, output string) {
if len(outputParts) == 2 {
cfg.File = outputParts[1]
} else {
if len(outputParts) > 2 || outputParts[0] != "file" {
panic("invalid output format")
}
cfg.File = "./waku.log"
}
}
@ -51,4 +54,6 @@ func InitLogger(encoding string, output string) {
logging.SetupLogging(cfg)
log = logging.Logger("gowaku").Desugar()
logging.SetAllLoggers(logging.LevelInfo)
}