mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
make context available as LogFormatterParams
This commit is contained in:
parent
e6288e90eb
commit
59500f7c36
@ -66,6 +66,8 @@ type LogFormatterParams struct {
|
|||||||
IsTerm bool
|
IsTerm bool
|
||||||
// BodySize is the size of the Response Body
|
// BodySize is the size of the Response Body
|
||||||
BodySize int
|
BodySize int
|
||||||
|
// Context is the Context associated with the request.
|
||||||
|
Context *Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.
|
// StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.
|
||||||
@ -227,6 +229,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
param := LogFormatterParams{
|
param := LogFormatterParams{
|
||||||
Request: c.Request,
|
Request: c.Request,
|
||||||
IsTerm: isTerm,
|
IsTerm: isTerm,
|
||||||
|
Context: c,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop timer
|
// Stop timer
|
||||||
|
@ -181,6 +181,7 @@ func TestLoggerWithFormatter(t *testing.T) {
|
|||||||
|
|
||||||
func TestLoggerWithConfigFormatting(t *testing.T) {
|
func TestLoggerWithConfigFormatting(t *testing.T) {
|
||||||
var gotParam LogFormatterParams
|
var gotParam LogFormatterParams
|
||||||
|
var gotCtx *Context
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
|
|
||||||
router := New()
|
router := New()
|
||||||
@ -204,6 +205,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
|||||||
router.GET("/example", func(c *Context) {
|
router.GET("/example", func(c *Context) {
|
||||||
// set dummy ClientIP
|
// set dummy ClientIP
|
||||||
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
|
c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")
|
||||||
|
gotCtx = c
|
||||||
})
|
})
|
||||||
performRequest(router, "GET", "/example?a=100")
|
performRequest(router, "GET", "/example?a=100")
|
||||||
|
|
||||||
@ -223,6 +225,8 @@ func TestLoggerWithConfigFormatting(t *testing.T) {
|
|||||||
assert.Equal(t, "GET", gotParam.Method)
|
assert.Equal(t, "GET", gotParam.Method)
|
||||||
assert.Equal(t, "/example?a=100", gotParam.Path)
|
assert.Equal(t, "/example?a=100", gotParam.Path)
|
||||||
assert.Empty(t, gotParam.ErrorMessage)
|
assert.Empty(t, gotParam.ErrorMessage)
|
||||||
|
assert.Empty(t, gotParam.ErrorMessage)
|
||||||
|
assert.Equal(t, gotCtx, gotParam.Context)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user