mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
add c.Writer.Status() readme details and example
This commit is contained in:
parent
a6c6ebce8f
commit
ad80fc5826
@ -286,6 +286,10 @@ func Logger() gin.HandlerFunc {
|
|||||||
// after request
|
// after request
|
||||||
latency := time.Since(t)
|
latency := time.Since(t)
|
||||||
log.Print(latency)
|
log.Print(latency)
|
||||||
|
|
||||||
|
// access the status we are sending
|
||||||
|
status := c.Writer.Status()
|
||||||
|
log.Println(status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
82
examples/example_ansi_logger.go
Normal file
82
examples/example_ansi_logger.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/mgutz/ansi"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
white = ansi.ColorCode("white+h:black")
|
||||||
|
red = ansi.ColorCode("red+h:black")
|
||||||
|
green = ansi.ColorCode("green+h:black")
|
||||||
|
yellow = ansi.ColorCode("yellow+h:black")
|
||||||
|
blue = ansi.ColorCode("blue+h:black")
|
||||||
|
reset = ansi.ColorCode("reset")
|
||||||
|
)
|
||||||
|
|
||||||
|
//
|
||||||
|
// Example of an extended ansi-colored logger using the
|
||||||
|
// ctx.Writer.Status() function
|
||||||
|
func logger(c *gin.Context) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
// save the IP of the requester
|
||||||
|
requester := c.Req.Header.Get("X-Real-IP")
|
||||||
|
|
||||||
|
// if the requester-header is empty, check the forwarded-header
|
||||||
|
if requester == "" {
|
||||||
|
requester = c.Req.Header.Get("X-Forwarded-For")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the requester is still empty, use the hard-coded address from the socket
|
||||||
|
if requester == "" {
|
||||||
|
requester = c.Req.RemoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... finally, log the fact we got a request
|
||||||
|
log.Printf("<-- %16s | %6s | %s\n", requester, c.Req.Method, c.Req.URL.Path)
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
var color string
|
||||||
|
if code := c.Writer.Status(); code >= 200 && code <= 299 {
|
||||||
|
color = green
|
||||||
|
} else if code >= 300 && code <= 399 {
|
||||||
|
color = white
|
||||||
|
} else if code >= 400 && code <= 499 {
|
||||||
|
color = yellow
|
||||||
|
} else {
|
||||||
|
color = red
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("--> %s%16s | %6d | %s | %s%s\n",
|
||||||
|
color,
|
||||||
|
requester, c.Writer.Status(), time.Since(start), c.Req.URL.Path,
|
||||||
|
reset,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.New()
|
||||||
|
r.Use(gin.Recovery())
|
||||||
|
r.Use(logger)
|
||||||
|
// or modify func.logger to return a handler and use:
|
||||||
|
// r.Use(logger())
|
||||||
|
|
||||||
|
// Ping test
|
||||||
|
r.GET("/:code", func(c *gin.Context) {
|
||||||
|
asInt, err := strconv.ParseInt(c.Params.ByName("code"), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
c.String(400, err.Error())
|
||||||
|
} else {
|
||||||
|
c.String(int(asInt), c.Params.ByName("code"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Listen and Server in 0.0.0.0:8080
|
||||||
|
r.Run(":8081")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user