Skip to content

Commit 69b4dc3

Browse files
committed
fix: bug #59
1 parent 3ed1e31 commit 69b4dc3

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

pkg/gin/middleware/logging.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"go.uber.org/zap"
1111
)
1212

13-
var contentMark = []byte(" ...... ")
14-
1513
var (
1614
// Print body max length
17-
defaultMaxLength = 300
15+
defaultMaxLength = 40
1816

1917
// default zap log
2018
defaultLogger, _ = zap.NewProduction()
@@ -25,6 +23,9 @@ var (
2523
"/pong": {},
2624
"/health": {},
2725
}
26+
27+
emptyBody = []byte("")
28+
contentMark = []byte(" ...... ")
2829
)
2930

3031
// Option set the gin logger options.
@@ -55,6 +56,9 @@ func (o *options) apply(opts ...Option) {
5556
// WithMaxLen logger content max length
5657
func WithMaxLen(maxLen int) Option {
5758
return func(o *options) {
59+
if maxLen < len(contentMark) {
60+
panic("maxLen should be greater than or equal to 8")
61+
}
5862
o.maxLength = maxLen
5963
}
6064
}
@@ -104,14 +108,36 @@ func (w bodyLogWriter) Write(b []byte) (int, error) {
104108
}
105109

106110
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
107-
func getBodyData(buf *bytes.Buffer, maxLen int) []byte {
111+
func getResponseBody(buf *bytes.Buffer, maxLen int) []byte {
108112
l := buf.Len()
109113
if l == 0 {
110114
return []byte("")
111-
} else if l <= maxLen {
112-
return buf.Bytes()[:l-1]
115+
} else if l > maxLen {
116+
l = maxLen
113117
}
114-
return append(bytes.Clone(buf.Bytes()[:maxLen]), contentMark...)
118+
119+
body := make([]byte, l)
120+
n, _ := buf.Read(body)
121+
if n == 0 {
122+
return emptyBody
123+
} else if n < maxLen {
124+
return body[:n-1]
125+
}
126+
return append(body[:maxLen-len(contentMark)], contentMark...)
127+
}
128+
129+
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
130+
func getRequestBody(buf *bytes.Buffer, maxLen int) []byte {
131+
l := buf.Len()
132+
if l == 0 {
133+
return []byte("")
134+
} else if l < maxLen {
135+
return buf.Bytes()
136+
}
137+
138+
body := make([]byte, maxLen)
139+
copy(body, buf.Bytes())
140+
return append(body[:maxLen-len(contentMark)], contentMark...)
115141
}
116142

117143
// Logging print request and response info
@@ -139,7 +165,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
139165
if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut || c.Request.Method == http.MethodPatch || c.Request.Method == http.MethodDelete {
140166
fields = append(fields,
141167
zap.Int("size", buf.Len()),
142-
zap.ByteString("body", getBodyData(&buf, o.maxLength)),
168+
zap.ByteString("body", getRequestBody(&buf, o.maxLength)),
143169
)
144170
}
145171

@@ -173,7 +199,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
173199
zap.String("url", c.Request.URL.Path),
174200
zap.Int64("time_us", time.Since(start).Microseconds()),
175201
zap.Int("size", newWriter.body.Len()),
176-
zap.ByteString("body", getBodyData(newWriter.body, o.maxLength)),
202+
zap.ByteString("body", getResponseBody(newWriter.body, o.maxLength)),
177203
}
178204
if reqID != "" {
179205
fields = append(fields, zap.String(ContextRequestIDKey, reqID))
@@ -221,6 +247,6 @@ func SimpleLog(opts ...Option) gin.HandlerFunc {
221247
if reqID != "" {
222248
fields = append(fields, zap.String(ContextRequestIDKey, reqID))
223249
}
224-
o.log.Info("[GIN]", fields...)
250+
o.log.Info("[GIN] message", fields...)
225251
}
226252
}

0 commit comments

Comments
 (0)