From f3640b1ad859aa0d407b7f67658254aaa5ecd45b Mon Sep 17 00:00:00 2001 From: paulwe Date: Sun, 1 Oct 2023 19:33:59 -0700 Subject: [PATCH 1/2] truncate bytes in proto logs --- logger/proto.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/logger/proto.go b/logger/proto.go index 3154f5c4..625f92d2 100644 --- a/logger/proto.go +++ b/logger/proto.go @@ -16,6 +16,7 @@ package logger import ( "encoding/base64" + "fmt" "strconv" "go.uber.org/zap/zapcore" @@ -106,7 +107,7 @@ func (p protoListMarshaller) MarshalLogArray(e zapcore.ArrayEncoder) error { case protoreflect.StringKind: e.AppendString(v.String()) case protoreflect.BytesKind: - e.AppendString(base64.RawStdEncoding.EncodeToString(v.Bytes())) + e.AppendString(marshalProtoBytes(v.Bytes())) case protoreflect.MessageKind: e.AppendObject(protoMarshaller{v.Message()}) } @@ -142,7 +143,7 @@ func marshalProtoField(k string, f protoreflect.FieldDescriptor, v protoreflect. } case protoreflect.BytesKind: if b := v.Bytes(); len(b) != 0 { - e.AddString(k, base64.RawStdEncoding.EncodeToString(b)) + e.AddString(k, marshalProtoBytes(b)) } case protoreflect.MessageKind: if m := v.Message(); m.IsValid() { @@ -150,3 +151,23 @@ func marshalProtoField(k string, f protoreflect.FieldDescriptor, v protoreflect. } } } + +func marshalProtoBytes(b []byte) string { + n := len(b) + if n > 64 { + n = 64 + } + s := base64.RawStdEncoding.EncodeToString(b[:n]) + switch { + case n <= 64: + return s + case n < 1<<10: + return fmt.Sprintf("%s... (%dbytes)", s, n) + case n < 1<<20: + return fmt.Sprintf("%s... (%.2fkB)", s, float64(n)/float64(1<<10)) + case n < 1<<30: + return fmt.Sprintf("%s... (%.2fMB)", s, float64(n)/float64(1<<20)) + default: + return fmt.Sprintf("%s... (%.2fGB)", s, float64(n)/float64(1<<30)) + } +} From 5b1807b209a25366839b543174b13f9e28bf5f07 Mon Sep 17 00:00:00 2001 From: paulwe Date: Sun, 1 Oct 2023 19:36:11 -0700 Subject: [PATCH 2/2] fix --- logger/proto.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logger/proto.go b/logger/proto.go index 625f92d2..a95fa868 100644 --- a/logger/proto.go +++ b/logger/proto.go @@ -155,9 +155,9 @@ func marshalProtoField(k string, f protoreflect.FieldDescriptor, v protoreflect. func marshalProtoBytes(b []byte) string { n := len(b) if n > 64 { - n = 64 + b = b[:64] } - s := base64.RawStdEncoding.EncodeToString(b[:n]) + s := base64.RawStdEncoding.EncodeToString(b) switch { case n <= 64: return s