mirror of https://github.com/gofiber/fiber.git
🖌 Add coloration in logger middleware
🖌 Add coloration in logger middlewarepull/609/head
commit
f50db73162
|
@ -31,6 +31,7 @@ type (
|
|||
// - url
|
||||
// - host
|
||||
// - method
|
||||
// - methodColored
|
||||
// - path
|
||||
// - protocol
|
||||
// - route
|
||||
|
@ -38,6 +39,7 @@ type (
|
|||
// - ua
|
||||
// - latency
|
||||
// - status
|
||||
// - statusColored
|
||||
// - body
|
||||
// - error
|
||||
// - bytesSent
|
||||
|
@ -85,6 +87,38 @@ const (
|
|||
LoggerTagQuery = "query:"
|
||||
LoggerTagForm = "form:"
|
||||
LoggerTagCookie = "cookie:"
|
||||
LoggerTagColorBlack = "black"
|
||||
LoggerTagColorRed = "red"
|
||||
LoggerTagColorGreen = "green"
|
||||
LoggerTagColorYellow = "yellow"
|
||||
LoggerTagColorBlue = "blue"
|
||||
LoggerTagColorMagenta = "magenta"
|
||||
LoggerTagColorCyan = "cyan"
|
||||
LoggerTagColorWhite = "white"
|
||||
LoggerTagColorReset = "resetColor"
|
||||
LoggerTagStatusColor = "statusColor"
|
||||
LoggerTagMethodColor = "methodColor"
|
||||
)
|
||||
|
||||
// NEW : Color variables
|
||||
const (
|
||||
cBlack = "\u001b[90m"
|
||||
cRed = "\u001b[91m"
|
||||
cGreen = "\u001b[92m"
|
||||
cYellow = "\u001b[93m"
|
||||
cBlue = "\u001b[94m"
|
||||
cMagenta = "\u001b[95m"
|
||||
cCyan = "\u001b[96m"
|
||||
cWhite = "\u001b[97m"
|
||||
cReset = "\u001b[0m"
|
||||
)
|
||||
|
||||
// for colorizing response status and request method
|
||||
var (
|
||||
statusColor string
|
||||
responseStatus int
|
||||
methodColor string
|
||||
requestMethod string
|
||||
)
|
||||
|
||||
// LoggerConfigDefault is the default config
|
||||
|
@ -222,6 +256,58 @@ func logger(config LoggerConfig) fiber.Handler {
|
|||
if c.Error() != nil {
|
||||
return buf.WriteString(c.Error().Error())
|
||||
}
|
||||
case LoggerTagColorBlack:
|
||||
return buf.WriteString(cBlack)
|
||||
case LoggerTagColorRed:
|
||||
return buf.WriteString(cRed)
|
||||
case LoggerTagColorGreen:
|
||||
return buf.WriteString(cGreen)
|
||||
case LoggerTagColorYellow:
|
||||
return buf.WriteString(cYellow)
|
||||
case LoggerTagColorBlue:
|
||||
return buf.WriteString(cBlue)
|
||||
case LoggerTagColorMagenta:
|
||||
return buf.WriteString(cMagenta)
|
||||
case LoggerTagColorCyan:
|
||||
return buf.WriteString(cCyan)
|
||||
case LoggerTagColorWhite:
|
||||
return buf.WriteString(cWhite)
|
||||
case LoggerTagColorReset:
|
||||
return buf.WriteString(cReset)
|
||||
case LoggerTagStatusColor:
|
||||
responseStatus = c.Fasthttp.Response.StatusCode()
|
||||
switch {
|
||||
case responseStatus >= 200 && responseStatus < 300:
|
||||
statusColor = cBlue
|
||||
case responseStatus >= 300 && responseStatus < 400:
|
||||
statusColor = cWhite
|
||||
case responseStatus >= 400 && responseStatus < 500:
|
||||
statusColor = cYellow
|
||||
default:
|
||||
statusColor = cRed
|
||||
}
|
||||
return buf.WriteString(statusColor)
|
||||
case LoggerTagMethodColor:
|
||||
requestMethod = c.Method()
|
||||
switch requestMethod {
|
||||
case "GET":
|
||||
methodColor = cBlue
|
||||
case "POST":
|
||||
methodColor = cCyan
|
||||
case "PUT":
|
||||
methodColor = cYellow
|
||||
case "DELETE":
|
||||
methodColor = cRed
|
||||
case "PATCH":
|
||||
methodColor = cGreen
|
||||
case "HEAD":
|
||||
methodColor = cMagenta
|
||||
case "OPTIONS":
|
||||
methodColor = cWhite
|
||||
default:
|
||||
methodColor = cReset
|
||||
}
|
||||
return buf.WriteString(methodColor)
|
||||
default:
|
||||
switch {
|
||||
case strings.HasPrefix(tag, LoggerTagHeader):
|
||||
|
|
|
@ -60,6 +60,7 @@ type LoggerConfig struct {
|
|||
// - url
|
||||
// - host
|
||||
// - method
|
||||
// - methodColor
|
||||
// - path
|
||||
// - protocol
|
||||
// - route
|
||||
|
@ -67,6 +68,7 @@ type LoggerConfig struct {
|
|||
// - ua
|
||||
// - latency
|
||||
// - status
|
||||
// - statusColor
|
||||
// - body
|
||||
// - error
|
||||
// - bytesSent
|
||||
|
@ -75,6 +77,7 @@ type LoggerConfig struct {
|
|||
// - query:<key>
|
||||
// - form:<key>
|
||||
// - cookie:<key>
|
||||
// - <color> - e.g. black, red, blue, yellow, cyan, magenta, white, resetColor
|
||||
//
|
||||
// Optional. Default: ${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n
|
||||
Format string
|
||||
|
|
|
@ -17,8 +17,8 @@ import (
|
|||
|
||||
// go test -run Test_Middleware_Logger
|
||||
func Test_Middleware_Logger(t *testing.T) {
|
||||
format := "${ip}-${ips}-${url}-${host}-${method}-${path}-${protocol}-${route}-${referer}-${ua}-${status}-${body}-${error}-${bytesSent}-${bytesReceived}-${header:header}-${query:query}-${form:form}-${cookie:cookie}"
|
||||
expect := "0.0.0.0--/test?query=query-example.com-POST-/test-http-/test-ref-ua-500-form=form-error-5-9-header-query-form-cookie"
|
||||
format := "${ip}-${ips}-${url}-${host}-${method}-${methodColor}${method}${resetColor}-${path}-${protocol}-${route}-${referer}-${ua}-${status}-${statusColor}${status}${resetColor}-${body}-${error}-${bytesSent}-${bytesReceived}-${header:header}-${query:query}-${form:form}-${cookie:cookie}-${black}-${red}-${green}-${yellow}-${blue}-${magenta}-${cyan}-${white}-${resetColor}"
|
||||
expect := "0.0.0.0--/test?query=query-example.com-POST-\u001b[96mPOST\u001b[0m-/test-http-/test-ref-ua-500-\u001b[91m500\u001b[0m-form=form-error-5-9-header-query-form-cookie-\u001b[90m-\u001b[91m-\u001b[92m-\u001b[93m-\u001b[94m-\u001b[95m-\u001b[96m-\u001b[97m-\u001b[0m"
|
||||
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
|
Loading…
Reference in New Issue