@@ -16,6 +16,7 @@ package logger
1616
1717import (
1818 "encoding/base64"
19+ "fmt"
1920 "strconv"
2021
2122 "go.uber.org/zap/zapcore"
@@ -106,7 +107,7 @@ func (p protoListMarshaller) MarshalLogArray(e zapcore.ArrayEncoder) error {
106107 case protoreflect .StringKind :
107108 e .AppendString (v .String ())
108109 case protoreflect .BytesKind :
109- e .AppendString (base64 . RawStdEncoding . EncodeToString (v .Bytes ()))
110+ e .AppendString (marshalProtoBytes (v .Bytes ()))
110111 case protoreflect .MessageKind :
111112 e .AppendObject (protoMarshaller {v .Message ()})
112113 }
@@ -142,11 +143,31 @@ func marshalProtoField(k string, f protoreflect.FieldDescriptor, v protoreflect.
142143 }
143144 case protoreflect .BytesKind :
144145 if b := v .Bytes (); len (b ) != 0 {
145- e .AddString (k , base64 . RawStdEncoding . EncodeToString (b ))
146+ e .AddString (k , marshalProtoBytes (b ))
146147 }
147148 case protoreflect .MessageKind :
148149 if m := v .Message (); m .IsValid () {
149150 e .AddObject (k , protoMarshaller {m })
150151 }
151152 }
152153}
154+
155+ func marshalProtoBytes (b []byte ) string {
156+ n := len (b )
157+ if n > 64 {
158+ b = b [:64 ]
159+ }
160+ s := base64 .RawStdEncoding .EncodeToString (b )
161+ switch {
162+ case n <= 64 :
163+ return s
164+ case n < 1 << 10 :
165+ return fmt .Sprintf ("%s... (%dbytes)" , s , n )
166+ case n < 1 << 20 :
167+ return fmt .Sprintf ("%s... (%.2fkB)" , s , float64 (n )/ float64 (1 << 10 ))
168+ case n < 1 << 30 :
169+ return fmt .Sprintf ("%s... (%.2fMB)" , s , float64 (n )/ float64 (1 << 20 ))
170+ default :
171+ return fmt .Sprintf ("%s... (%.2fGB)" , s , float64 (n )/ float64 (1 << 30 ))
172+ }
173+ }
0 commit comments