定义路由日志的格式

    如果你想要以指定的格式(例如 JSON,key values 或其他格式)记录信息,则可以使用 指定格式。在下面的示例中,我们使用标准日志包记录所有路由,但你可以使用其他满足你需求的日志工具。

    1. import (
    2. "log"
    3. "net/http"
    4. "github.com/gin-gonic/gin"
    5. )
    6. func main() {
    7. gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
    8. log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
    9. }
    10. r.POST("/foo", func(c *gin.Context) {
    11. c.JSON(http.StatusOK, "foo")
    12. })
    13. c.JSON(http.StatusOK, "bar")
    14. })
    15. r.GET("/status", func(c *gin.Context) {
    16. c.JSON(http.StatusOK, "ok")
    17. })
    18. // 监听并在 0.0.0.0:8080 上启动服务
    19. }