Skip to content
Myr

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

2 files+51 added−1 removed

modified:debug.go+7 added-1 removed
18 unmodified lines
19func IsDebugging() bool {20 return ginMode == debugCode21}2223var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)2425func 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}3637func debugPrintLoadTemplate(tmpl *template.Template) {
49 unmodified lines
modified:README.md+44 added
56 unmodified lines
57 - [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)6465## Installation
1770 unmodified lines
1836 r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")1837}1838```18391840### Define format for the log of routes18411842The 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```18481849If 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`.1850In the example below, we log all routes with standard log package but you can use another log tools that suits of your needs.1851```go1852import (1853 "log"1854 "net/http"18551856 "github.com/gin-gonic/gin"1857)18581859func 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 }18641865 r.POST("/foo", func(c *gin.Context) {1866 c.JSON(http.StatusOK, "foo")1867 })18681869 r.GET("/bar", func(c *gin.Context) {1870 c.JSON(http.StatusOK, "bar")1871 })18721873 r.GET("/status", func(c *gin.Context) {1874 c.JSON(http.StatusOK, "ok")1875 })18761877 // Listen and Server in http://0.0.0.0:80801878 r.Run()1879}1880```188118821883## Testing18841885The `net/http/httptest` package is preferable way for HTTP testing.1886
49 unmodified lines