I'm building logger like this
config := zap.NewDevelopmentConfig()
logger, err := config.Build()
if err != nil {
log.Fatal(err)
}
Imagine I have structure Request
type Request {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name`
Surname string `protobuf:"bytes,1,opt,name=surname`
}
After I'm trying to log grpc request, important that request do not contains Surname field
logger.Infof("got request: %v", log)
Output message is
got request: name:"Bob"
I want to all the structure with zero value fields, like this
got request: name:"Bob" surname:""
Is in zap any config about data types otput
It looks like
logger.Sugar()was omitted from the example —zap.Loggerdoesn't have anInfofmethod.SugaredLogger.Infofformats the objects according tofmt.Sprintf. You'll need to implement your ownStringerto customize the representation. Given that the example struct is a generated protobuf, you could embed the type:If you care about performance, you should use
zap.Loggerinstead.It's not quite clear from your question whether you always want to include empty string values or not. If you want them, then use
logger.Info(msg, zap.Any("request", request)). If you don't, then implementzapcore.ObjectMarshalerinstead offmt.Stringer.