Let's user define how he wants to log his routes (eg. JSON, key value, or something else) (#1553) (#1555)
This commit is contained in:
		
							
								
								
									
										44
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								README.md
									
									
									
									
									
								
							@ -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.
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										8
									
								
								debug.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								debug.go
									
									
									
									
									
								
							@ -20,11 +20,17 @@ func IsDebugging() bool {
 | 
				
			|||||||
	return ginMode == debugCode
 | 
						return ginMode == debugCode
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
 | 
					func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
 | 
				
			||||||
	if IsDebugging() {
 | 
						if IsDebugging() {
 | 
				
			||||||
		nuHandlers := len(handlers)
 | 
							nuHandlers := len(handlers)
 | 
				
			||||||
		handlerName := nameOfFunction(handlers.Last())
 | 
							handlerName := nameOfFunction(handlers.Last())
 | 
				
			||||||
		debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
 | 
							if DebugPrintRouteFunc == nil {
 | 
				
			||||||
 | 
								debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user