|
@@ -58,6 +58,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
|
|
|
- [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
|
|
- [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
|
|
|
- [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
|
|
- [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
|
|
|
- [http2 server push](#http2-server-push)
|
|
- [http2 server push](#http2-server-push)
|
|
|
|
|
+ - [Define format for the log of routes](#define-format-for-the-log-of-routes)
|
|
|
- [Testing](#testing)
|
|
- [Testing](#testing)
|
|
|
- [Users](#users)
|
|
- [Users](#users)
|
|
|
|
|
|
|
@@ -1836,6 +1837,49 @@ func main() {
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+### Define format for the log of routes
|
|
|
|
|
+
|
|
|
|
|
+The default log of routes is:
|
|
|
|
|
+```
|
|
|
|
|
+[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)
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+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.
|
|
|
|
|
+```go
|
|
|
|
|
+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()
|
|
|
|
|
+}
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
## Testing
|
|
## Testing
|
|
|
|
|
|
|
|
The `net/http/httptest` package is preferable way for HTTP testing.
|
|
The `net/http/httptest` package is preferable way for HTTP testing.
|