Skip to content

Commit

Permalink
chore: allow setting custom logger name and change debug level to sto…
Browse files Browse the repository at this point in the history
…reV3 client
  • Loading branch information
richard-ramos committed May 31, 2024
1 parent 8115ec7 commit 37775b6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/waku/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func nonRecoverError(err error) error {
func Execute(options NodeOptions) error {
// Set encoding for logs (console, json, ...)
// Note that libp2p reads the encoding from GOLOG_LOG_FMT env var.
utils.InitLogger(options.LogEncoding, options.LogOutput)
utils.InitLogger(options.LogEncoding, options.LogOutput, "gowaku")

hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", options.Address, options.Port))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/chat2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
app := &cli.App{
Flags: getFlags(),
Action: func(c *cli.Context) error {
utils.InitLogger("console", "file:chat2.log")
utils.InitLogger("console", "file:chat2.log", "chat2")

lvl, err := logging.LevelFromString(options.LogLevel)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions waku/v2/protocol/store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ func (s *WakuStore) next(ctx context.Context, r *Result) (*Result, error) {
}

func (s *WakuStore) queryFrom(ctx context.Context, storeRequest *pb.StoreQueryRequest, selectedPeer peer.ID) (*pb.StoreQueryResponse, error) {
logger := s.log.With(logging.HostID("peer", selectedPeer))
logger.Info("sending store request")
logger := s.log.With(logging.HostID("peer", selectedPeer), zap.String("requestId", hex.EncodeToString([]byte(storeRequest.RequestId))))

logger.Debug("sending store request")

stream, err := s.h.NewStream(ctx, selectedPeer, StoreQueryID_v300)
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions waku/v2/utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ var log *zap.Logger
var messageLoggers map[string]*zap.Logger

// Logger creates a zap.Logger with some reasonable defaults
func Logger() *zap.Logger {
func Logger(name ...string) *zap.Logger {
loggerName := "gowaku"
if len(name) != 0 {
loggerName = name[0]
}

if log == nil {
InitLogger("console", "stdout")
InitLogger("console", "stdout", loggerName)
}
return log
}
Expand All @@ -34,7 +39,7 @@ func MessagesLogger(prefix string) *zap.Logger {
}

// InitLogger initializes a global logger using an specific encoding
func InitLogger(encoding string, output string) {
func InitLogger(encoding string, output string, name string) {
cfg := logging.GetConfig()

if encoding == "json" {
Expand Down Expand Up @@ -72,5 +77,5 @@ func InitLogger(encoding string, output string) {

logging.SetupLogging(cfg)

log = logging.Logger("gowaku").Desugar()
log = logging.Logger(name).Desugar()
}

0 comments on commit 37775b6

Please sign in to comment.