mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 14:03:53 +00:00
commit
b03b596a59
@ -119,29 +119,31 @@ var ConfigDefault = Config{
|
|||||||
```go
|
```go
|
||||||
// Logger variables
|
// Logger variables
|
||||||
const (
|
const (
|
||||||
TagPid = "pid"
|
TagPid = "pid"
|
||||||
TagTime = "time"
|
TagTime = "time"
|
||||||
TagReferer = "referer"
|
TagReferer = "referer"
|
||||||
TagProtocol = "protocol"
|
TagProtocol = "protocol"
|
||||||
TagIP = "ip"
|
TagIP = "ip"
|
||||||
TagIPs = "ips"
|
TagIPs = "ips"
|
||||||
TagHost = "host"
|
TagHost = "host"
|
||||||
TagMethod = "method"
|
TagMethod = "method"
|
||||||
TagPath = "path"
|
TagPath = "path"
|
||||||
TagURL = "url"
|
TagURL = "url"
|
||||||
TagUA = "ua"
|
TagUA = "ua"
|
||||||
TagLatency = "latency"
|
TagLatency = "latency"
|
||||||
TagStatus = "status" // response status
|
TagStatus = "status" // response status
|
||||||
TagBody = "body" // request body
|
TagResBody = "resBody" // response body
|
||||||
TagBytesSent = "bytesSent"
|
TagQueryStringParams = "queryParams" // request query parameters
|
||||||
TagBytesReceived = "bytesReceived"
|
TagBody = "body" // request body
|
||||||
TagRoute = "route"
|
TagBytesSent = "bytesSent"
|
||||||
TagError = "error"
|
TagBytesReceived = "bytesReceived"
|
||||||
TagHeader = "header:" // request header
|
TagRoute = "route"
|
||||||
TagQuery = "query:" // request query
|
TagError = "error"
|
||||||
TagForm = "form:" // request form
|
TagHeader = "header:" // request header
|
||||||
TagCookie = "cookie:" // request cookie
|
TagQuery = "query:" // request query
|
||||||
TagLocals = "locals:"
|
TagForm = "form:" // request form
|
||||||
|
TagCookie = "cookie:" // request cookie
|
||||||
|
TagLocals = "locals:"
|
||||||
|
|
||||||
// colors
|
// colors
|
||||||
TagBlack = "black"
|
TagBlack = "black"
|
||||||
|
@ -20,38 +20,40 @@ import (
|
|||||||
|
|
||||||
// Logger variables
|
// Logger variables
|
||||||
const (
|
const (
|
||||||
TagPid = "pid"
|
TagPid = "pid"
|
||||||
TagTime = "time"
|
TagTime = "time"
|
||||||
TagReferer = "referer"
|
TagReferer = "referer"
|
||||||
TagProtocol = "protocol"
|
TagProtocol = "protocol"
|
||||||
TagIP = "ip"
|
TagIP = "ip"
|
||||||
TagIPs = "ips"
|
TagIPs = "ips"
|
||||||
TagHost = "host"
|
TagHost = "host"
|
||||||
TagMethod = "method"
|
TagMethod = "method"
|
||||||
TagPath = "path"
|
TagPath = "path"
|
||||||
TagURL = "url"
|
TagURL = "url"
|
||||||
TagUA = "ua"
|
TagUA = "ua"
|
||||||
TagLatency = "latency"
|
TagLatency = "latency"
|
||||||
TagStatus = "status"
|
TagStatus = "status"
|
||||||
TagBody = "body"
|
TagResBody = "resBody"
|
||||||
TagBytesSent = "bytesSent"
|
TagQueryStringParams = "queryParams"
|
||||||
TagBytesReceived = "bytesReceived"
|
TagBody = "body"
|
||||||
TagRoute = "route"
|
TagBytesSent = "bytesSent"
|
||||||
TagError = "error"
|
TagBytesReceived = "bytesReceived"
|
||||||
TagHeader = "header:"
|
TagRoute = "route"
|
||||||
TagLocals = "locals:"
|
TagError = "error"
|
||||||
TagQuery = "query:"
|
TagHeader = "header:"
|
||||||
TagForm = "form:"
|
TagLocals = "locals:"
|
||||||
TagCookie = "cookie:"
|
TagQuery = "query:"
|
||||||
TagBlack = "black"
|
TagForm = "form:"
|
||||||
TagRed = "red"
|
TagCookie = "cookie:"
|
||||||
TagGreen = "green"
|
TagBlack = "black"
|
||||||
TagYellow = "yellow"
|
TagRed = "red"
|
||||||
TagBlue = "blue"
|
TagGreen = "green"
|
||||||
TagMagenta = "magenta"
|
TagYellow = "yellow"
|
||||||
TagCyan = "cyan"
|
TagBlue = "blue"
|
||||||
TagWhite = "white"
|
TagMagenta = "magenta"
|
||||||
TagReset = "reset"
|
TagCyan = "cyan"
|
||||||
|
TagWhite = "white"
|
||||||
|
TagReset = "reset"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Color values
|
// Color values
|
||||||
@ -230,6 +232,10 @@ func New(config ...Config) fiber.Handler {
|
|||||||
return buf.WriteString(c.Route().Path)
|
return buf.WriteString(c.Route().Path)
|
||||||
case TagStatus:
|
case TagStatus:
|
||||||
return appendInt(buf, c.Response().StatusCode())
|
return appendInt(buf, c.Response().StatusCode())
|
||||||
|
case TagResBody:
|
||||||
|
return buf.Write(c.Response().Body())
|
||||||
|
case TagQueryStringParams:
|
||||||
|
return buf.WriteString(c.Request().URI().QueryArgs().String())
|
||||||
case TagMethod:
|
case TagMethod:
|
||||||
return buf.WriteString(c.Method())
|
return buf.WriteString(c.Method())
|
||||||
case TagBlack:
|
case TagBlack:
|
||||||
|
@ -141,14 +141,67 @@ func Test_Logger_All(t *testing.T) {
|
|||||||
Output: buf,
|
Output: buf,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
resp, err := app.Test(httptest.NewRequest("GET", "/?foo=bar", nil))
|
||||||
utils.AssertEqual(t, nil, err)
|
utils.AssertEqual(t, nil, err)
|
||||||
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||||
|
|
||||||
expected := fmt.Sprintf("%dhttp0.0.0.0example.com//%s%s%s%s%s%s%s%s%s-", os.Getpid(), cBlack, cRed, cGreen, cYellow, cBlue, cMagenta, cCyan, cWhite, cReset)
|
expected := fmt.Sprintf("%dhttp0.0.0.0example.com/?foo=bar/%s%s%s%s%s%s%s%s%s-", os.Getpid(), cBlack, cRed, cGreen, cYellow, cBlue, cMagenta, cCyan, cWhite, cReset)
|
||||||
utils.AssertEqual(t, expected, buf.String())
|
utils.AssertEqual(t, expected, buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go test -run Test_Query_Params
|
||||||
|
func Test_Query_Params(t *testing.T) {
|
||||||
|
buf := bytebufferpool.Get()
|
||||||
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
|
app := fiber.New()
|
||||||
|
app.Use(New(Config{
|
||||||
|
Format: "${queryParams}",
|
||||||
|
Output: buf,
|
||||||
|
}))
|
||||||
|
|
||||||
|
resp, err := app.Test(httptest.NewRequest("GET", "/?foo=bar&baz=moz", nil))
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||||
|
|
||||||
|
expected := fmt.Sprintf("foo=bar&baz=moz")
|
||||||
|
utils.AssertEqual(t, expected, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// go test -run Test_Response_Body
|
||||||
|
func Test_Response_Body(t *testing.T) {
|
||||||
|
buf := bytebufferpool.Get()
|
||||||
|
defer bytebufferpool.Put(buf)
|
||||||
|
|
||||||
|
app := fiber.New()
|
||||||
|
app.Use(New(Config{
|
||||||
|
Format: "${resBody}",
|
||||||
|
Output: buf,
|
||||||
|
}))
|
||||||
|
|
||||||
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
|
return c.SendString("Sample response body")
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Post("/test", func(c *fiber.Ctx) error {
|
||||||
|
return c.Send([]byte("Post in test"))
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := app.Test(httptest.NewRequest("GET", "/", nil))
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
expectedGetResponse := fmt.Sprintf("Sample response body")
|
||||||
|
utils.AssertEqual(t, expectedGetResponse, buf.String())
|
||||||
|
|
||||||
|
buf.Reset() // Reset buffer to test POST
|
||||||
|
|
||||||
|
_, err = app.Test(httptest.NewRequest("POST", "/test", nil))
|
||||||
|
utils.AssertEqual(t, nil, err)
|
||||||
|
|
||||||
|
expectedPostResponse := fmt.Sprintf("Post in test")
|
||||||
|
utils.AssertEqual(t, expectedPostResponse, buf.String())
|
||||||
|
}
|
||||||
|
|
||||||
// go test -run Test_Logger_AppendUint
|
// go test -run Test_Logger_AppendUint
|
||||||
func Test_Logger_AppendUint(t *testing.T) {
|
func Test_Logger_AppendUint(t *testing.T) {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user