go-spacemesh icon indicating copy to clipboard operation
go-spacemesh copied to clipboard

logging: use zap.Inline and zapcore.ObjectMarshaller interface to marshal structs

Open dshulyak opened this issue 3 years ago • 0 comments

Using zap is supposed to be fast and not allocation heavy, this benefit is negated by suboptimal patterns that are currently used in the codebase. More importantly all heavy allocations should be avoided when log is not supposed to be printed, this will allow to keep debug logs in hot loops, where they were removed simply because they are too costly even when Debug weren't enabled.

Refactor Fields() method on multiple objects to use ObjectMarshaller interface from zap library.

type ObjectMarshaler interface {
	MarshalLogObject(ObjectEncoder) error
}

This will allow to use zap.Inline to marshal complex structures such as Block, Context, ATX and so on, e.g

block := &types.Block{}
logger.With().Info("block info", log.Inline(block))
// or
logger.With().Info("block info", log.Struct(block))

dshulyak avatar Nov 23 '21 06:11 dshulyak