mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Merge 387e102b37b3bb0e40cd7d741175a5bfca75e4e9 into 857db39f82fb82456af2906ccea972ae1d65ff57
This commit is contained in:
commit
003ab6e835
14
logger.go
14
logger.go
@ -5,6 +5,7 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -47,6 +48,10 @@ type LoggerConfig struct {
|
||||
// SkipPaths is an url path array which logs are not written.
|
||||
// Optional.
|
||||
SkipPaths []string
|
||||
|
||||
// RequestBody is a bool to enable request body logging
|
||||
// Optional. Default value is false
|
||||
RequestBody bool
|
||||
}
|
||||
|
||||
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
||||
@ -238,11 +243,20 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
||||
path := c.Request.URL.Path
|
||||
raw := c.Request.URL.RawQuery
|
||||
|
||||
var body []byte
|
||||
if conf.RequestBody {
|
||||
body, _ = io.ReadAll(c.Request.Body)
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
|
||||
// Process request
|
||||
c.Next()
|
||||
|
||||
// Log only when path is not being skipped
|
||||
if _, ok := skip[path]; !ok {
|
||||
if conf.RequestBody {
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
param := LogFormatterParams{
|
||||
Request: c.Request,
|
||||
isTerm: isTerm,
|
||||
|
@ -5,8 +5,10 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -182,6 +184,7 @@ func TestLoggerWithFormatter(t *testing.T) {
|
||||
func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||
var gotParam LogFormatterParams
|
||||
var gotKeys map[string]any
|
||||
var gotBody []byte
|
||||
buffer := new(strings.Builder)
|
||||
|
||||
router := New()
|
||||
@ -189,11 +192,13 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||
|
||||
router.Use(LoggerWithConfig(LoggerConfig{
|
||||
Output: buffer,
|
||||
RequestBody: true,
|
||||
Formatter: func(param LogFormatterParams) string {
|
||||
// for assert test
|
||||
gotParam = param
|
||||
gotBody, _ = io.ReadAll(param.Request.Body)
|
||||
|
||||
return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %s\n%s",
|
||||
return fmt.Sprintf("[FORMATTER TEST] %v | %3d | %13v | %15s | %-7s %s %s\n%s",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
@ -201,6 +206,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.ErrorMessage,
|
||||
string(gotBody),
|
||||
)
|
||||
},
|
||||
}))
|
||||
@ -229,6 +235,16 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||
assert.Equal(t, "/example?a=100", gotParam.Path)
|
||||
assert.Empty(t, gotParam.ErrorMessage)
|
||||
assert.Equal(t, gotKeys, gotParam.Keys)
|
||||
|
||||
router.POST("/example", func(c *Context) {
|
||||
// set dummy ClientIP
|
||||
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
|
||||
time.Sleep(time.Millisecond)
|
||||
})
|
||||
PerformBodyRequest(router, "POST", "/example", []header{}, bytes.NewBufferString(`{"name":"test"}`))
|
||||
|
||||
// LogFormatterParams post body test
|
||||
assert.Equal(t, string(gotBody), `{"name":"test"}`)
|
||||
}
|
||||
|
||||
func TestDefaultLogFormatter(t *testing.T) {
|
||||
|
@ -6,6 +6,7 @@ package gin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -30,7 +31,15 @@ func PerformRequest(r http.Handler, method, path string, headers ...header) *htt
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
func PerformBodyRequest(r http.Handler, method, path string, headers []header, body io.Reader) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, path, body)
|
||||
for _, h := range headers {
|
||||
req.Header.Add(h.Key, h.Value)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
func testRouteOK(method string, t *testing.T) {
|
||||
passed := false
|
||||
passedAny := false
|
||||
|
Loading…
x
Reference in New Issue
Block a user