Define format for the log of routes

The default log of routes is:

  1. [GIN-debug] POST /foo --> main.main.func1 (3 handlers)
  2. [GIN-debug] GET /bar --> main.main.func2 (3 handlers)
  3. [GIN-debug] GET /status --> main.main.func3 (3 handlers)

If you want to log this information in given format (e.g. JSON, key values or something else), then you can define this format with gin.DebugPrintRouteFunc. In the example below, we log all routes with standard log package but you can use another log tools that suits of your needs.

  1. import (
  2. "log"
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. r := gin.Default()
  8. gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
  9. log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
  10. }
  11. r.POST("/foo", func(c *gin.Context) {
  12. c.JSON(http.StatusOK, "foo")
  13. })
  14. r.GET("/bar", func(c *gin.Context) {
  15. c.JSON(http.StatusOK, "bar")
  16. })
  17. r.GET("/status", func(c *gin.Context) {
  18. c.JSON(http.StatusOK, "ok")
  19. })
  20. // Listen and Server in http://0.0.0.0:8080
  21. r.Run()
  22. }

Last modified March 7, 2020 : add blog dir (#115) (f46734b)