gin自定義路由日志的格式
默認的路由日志是這樣的:
[GIN-debug] POST /foo --> main.main.func1 (3 handlers)
[GIN-debug] GET /bar --> main.main.func2 (3 handlers)
[GIN-debug] GET /status --> main.main.func3 (3 handlers)
如果你想以給定的格式記錄這些資訊(例如 JSON,鍵值對或其他格式),你可以使用gin.DebugPrintRouteFunc來定義格式,在下面的示例中,我們使用標準日志包記錄路由日志,你可以使用其他適合你需求的日志工具
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
}
r.POST("/foo", func(c *gin.Context) {
c.JSON(http.StatusOK, "foo")
})
r.GET("/bar", func(c *gin.Context) {
c.JSON(http.StatusOK, "bar")
})
r.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, "ok")
})
// Listen and Server in http://0.0.0.0:8080
r.Run()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/200561.html
標籤:其他
上一篇:邊緣計算與物聯網
