github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/grpclogging/fields.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package grpclogging 8 9 import ( 10 "github.com/gogo/protobuf/proto" 11 "github.com/golang/protobuf/jsonpb" 12 "go.uber.org/zap" 13 "go.uber.org/zap/zapcore" 14 ) 15 16 type protoMarshaler struct { 17 jsonpb.Marshaler 18 message proto.Message 19 } 20 21 func (m *protoMarshaler) MarshalJSON() ([]byte, error) { 22 out, err := m.Marshaler.MarshalToString(m.message) 23 if err != nil { 24 return nil, err 25 } 26 return []byte(out), nil 27 } 28 29 func ProtoMessage(key string, val interface{}) zapcore.Field { 30 if pm, ok := val.(proto.Message); ok { 31 return zap.Reflect(key, &protoMarshaler{message: pm}) 32 } 33 return zap.Any(key, val) 34 } 35 36 func Error(err error) zapcore.Field { 37 if err == nil { 38 return zap.Skip() 39 } 40 41 // Wrap the error so it no longer implements fmt.Formatter. This will prevent 42 // zap from adding the "verboseError" field to the log record that includes a 43 // full stack trace. 44 return zap.Error(struct{ error }{err}) 45 }