mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 21:43:54 +00:00
commit
b03b596a59
@ -119,29 +119,31 @@ var ConfigDefault = Config{
|
||||
```go
|
||||
// Logger variables
|
||||
const (
|
||||
TagPid = "pid"
|
||||
TagTime = "time"
|
||||
TagReferer = "referer"
|
||||
TagProtocol = "protocol"
|
||||
TagIP = "ip"
|
||||
TagIPs = "ips"
|
||||
TagHost = "host"
|
||||
TagMethod = "method"
|
||||
TagPath = "path"
|
||||
TagURL = "url"
|
||||
TagUA = "ua"
|
||||
TagLatency = "latency"
|
||||
TagStatus = "status" // response status
|
||||
TagBody = "body" // request body
|
||||
TagBytesSent = "bytesSent"
|
||||
TagBytesReceived = "bytesReceived"
|
||||
TagRoute = "route"
|
||||
TagError = "error"
|
||||
TagHeader = "header:" // request header
|
||||
TagQuery = "query:" // request query
|
||||
TagForm = "form:" // request form
|
||||
TagCookie = "cookie:" // request cookie
|
||||
TagLocals = "locals:"
|
||||
TagPid = "pid"
|
||||
TagTime = "time"
|
||||
TagReferer = "referer"
|
||||
TagProtocol = "protocol"
|
||||
TagIP = "ip"
|
||||
TagIPs = "ips"
|
||||
TagHost = "host"
|
||||
TagMethod = "method"
|
||||
TagPath = "path"
|
||||
TagURL = "url"
|
||||
TagUA = "ua"
|
||||
TagLatency = "latency"
|
||||
TagStatus = "status" // response status
|
||||
TagResBody = "resBody" // response body
|
||||
TagQueryStringParams = "queryParams" // request query parameters
|
||||
TagBody = "body" // request body
|
||||
TagBytesSent = "bytesSent"
|
||||
TagBytesReceived = "bytesReceived"
|
||||
TagRoute = "route"
|
||||
TagError = "error"
|
||||
TagHeader = "header:" // request header
|
||||
TagQuery = "query:" // request query
|
||||
TagForm = "form:" // request form
|
||||
TagCookie = "cookie:" // request cookie
|
||||
TagLocals = "locals:"
|
||||
|
||||
// colors
|
||||
TagBlack = "black"
|
||||
@ -154,4 +156,4 @@ const (
|
||||
TagWhite = "white"
|
||||
TagReset = "reset"
|
||||
)
|
||||
```
|
||||
```
|
||||
|
@ -20,38 +20,40 @@ import (
|
||||
|
||||
// Logger variables
|
||||
const (
|
||||
TagPid = "pid"
|
||||
TagTime = "time"
|
||||
TagReferer = "referer"
|
||||
TagProtocol = "protocol"
|
||||
TagIP = "ip"
|
||||
TagIPs = "ips"
|
||||
TagHost = "host"
|
||||
TagMethod = "method"
|
||||
TagPath = "path"
|
||||
TagURL = "url"
|
||||
TagUA = "ua"
|
||||
TagLatency = "latency"
|
||||
TagStatus = "status"
|
||||
TagBody = "body"
|
||||
TagBytesSent = "bytesSent"
|
||||
TagBytesReceived = "bytesReceived"
|
||||
TagRoute = "route"
|
||||
TagError = "error"
|
||||
TagHeader = "header:"
|
||||
TagLocals = "locals:"
|
||||
TagQuery = "query:"
|
||||
TagForm = "form:"
|
||||
TagCookie = "cookie:"
|
||||
TagBlack = "black"
|
||||
TagRed = "red"
|
||||
TagGreen = "green"
|
||||
TagYellow = "yellow"
|
||||
TagBlue = "blue"
|
||||
TagMagenta = "magenta"
|
||||
TagCyan = "cyan"
|
||||
TagWhite = "white"
|
||||
TagReset = "reset"
|
||||
TagPid = "pid"
|
||||
TagTime = "time"
|
||||
TagReferer = "referer"
|
||||
TagProtocol = "protocol"
|
||||
TagIP = "ip"
|
||||
TagIPs = "ips"
|
||||
TagHost = "host"
|
||||
TagMethod = "method"
|
||||
TagPath = "path"
|
||||
TagURL = "url"
|
||||
TagUA = "ua"
|
||||
TagLatency = "latency"
|
||||
TagStatus = "status"
|
||||
TagResBody = "resBody"
|
||||
TagQueryStringParams = "queryParams"
|
||||
TagBody = "body"
|
||||
TagBytesSent = "bytesSent"
|
||||
TagBytesReceived = "bytesReceived"
|
||||
TagRoute = "route"
|
||||
TagError = "error"
|
||||
TagHeader = "header:"
|
||||
TagLocals = "locals:"
|
||||
TagQuery = "query:"
|
||||
TagForm = "form:"
|
||||
TagCookie = "cookie:"
|
||||
TagBlack = "black"
|
||||
TagRed = "red"
|
||||
TagGreen = "green"
|
||||
TagYellow = "yellow"
|
||||
TagBlue = "blue"
|
||||
TagMagenta = "magenta"
|
||||
TagCyan = "cyan"
|
||||
TagWhite = "white"
|
||||
TagReset = "reset"
|
||||
)
|
||||
|
||||
// Color values
|
||||
@ -230,6 +232,10 @@ func New(config ...Config) fiber.Handler {
|
||||
return buf.WriteString(c.Route().Path)
|
||||
case TagStatus:
|
||||
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:
|
||||
return buf.WriteString(c.Method())
|
||||
case TagBlack:
|
||||
|
@ -141,14 +141,67 @@ func Test_Logger_All(t *testing.T) {
|
||||
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, 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())
|
||||
}
|
||||
|
||||
// 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
|
||||
func Test_Logger_AppendUint(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
Loading…
x
Reference in New Issue
Block a user