Let's user define how he wants to log his routes (eg. JSON, key value, or something else) (#1553) (#1555)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jérôme Laforge · committed by 田欧 · parent 7c7f703 · Browse files · 2 files·+51 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+51 added−1 removed
- other:+51 added−1 removed
Provenance
- Commit
90c680e- Tree
14108ff- Author
Jérôme Laforge <jerome-laforge@users.noreply.github.com>- Committer
田欧 <thinkerou@gmail.com>- Parent
7c7f703
2 files changed+51 added−1 removed
modified:debug.go+7 added-1 removed
18 unmodified lines19 func IsDebugging() bool {20 return ginMode == debugCode21 }22 23+var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)24+25 func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {26 if IsDebugging() {27 nuHandlers := len(handlers)28 handlerName := nameOfFunction(handlers.Last())29+ if DebugPrintRouteFunc == nil {27- debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)30+ debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)31+ } else {32+ DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)33+ }34 }35 }36 37 func debugPrintLoadTemplate(tmpl *template.Template) {49 unmodified linesmodified:README.md+44 added
56 unmodified lines57 - [Build a single binary with templates](#build-a-single-binary-with-templates)58 - [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)59 - [Try to bind body into different structs](#try-to-bind-body-into-different-structs)60 - [http2 server push](#http2-server-push)61+ - [Define format for the log of routes](#define-format-for-the-log-of-routes)62 - [Testing](#testing)63 - [Users](#users)64 65 ## Installation1770 unmodified lines1836 r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")1837 }1838 ```1839 1840+### Define format for the log of routes1841+1842+The default log of routes is:1843+```1844+[GIN-debug] POST /foo --> main.main.func1 (3 handlers)1845+[GIN-debug] GET /bar --> main.main.func2 (3 handlers)1846+[GIN-debug] GET /status --> main.main.func3 (3 handlers)1847+```1848+1849+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`.1850+In the example below, we log all routes with standard log package but you can use another log tools that suits of your needs.1851+```go1852+import (1853+ "log"1854+ "net/http"1855+1856+ "github.com/gin-gonic/gin"1857+)1858+1859+func main() {1860+ r := gin.Default()1861+ gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {1862+ log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)1863+ }1864+1865+ r.POST("/foo", func(c *gin.Context) {1866+ c.JSON(http.StatusOK, "foo")1867+ })1868+1869+ r.GET("/bar", func(c *gin.Context) {1870+ c.JSON(http.StatusOK, "bar")1871+ })1872+1873+ r.GET("/status", func(c *gin.Context) {1874+ c.JSON(http.StatusOK, "ok")1875+ })1876+1877+ // Listen and Server in http://0.0.0.0:80801878+ r.Run()1879+}1880+```1881+1882+1883 ## Testing1884 1885 The `net/http/httptest` package is preferable way for HTTP testing.1886 49 unmodified lines