From 346ca2a21911e588f3692646ebdac93ac964c7ae Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 26 Apr 2020 17:51:27 +0300 Subject: [PATCH] route logging improvement: group by methods Former-commit-id: ad884991433a244dc76bdad7314d98a5c204dac6 --- _examples/mvc/grpc-compatible/main.go | 2 +- _examples/routing/main.go | 2 ++ _examples/routing/overview/main.go | 2 +- core/router/api_builder.go | 8 ++++---- core/router/handler.go | 27 ++++++++++++++++++++++++++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/_examples/mvc/grpc-compatible/main.go b/_examples/mvc/grpc-compatible/main.go index 667b78c9..6d790e31 100644 --- a/_examples/mvc/grpc-compatible/main.go +++ b/_examples/mvc/grpc-compatible/main.go @@ -32,7 +32,7 @@ func main() { func newApp() *iris.Application { app := iris.New() // app.Configure(iris.WithLowercaseRouting) // OPTIONAL. - app.Logger().SetLevel("debug").SetTimeFormat("") + app.Logger().SetLevel("debug") app.Get("/", func(ctx iris.Context) { ctx.HTML("

Index Page

") diff --git a/_examples/routing/main.go b/_examples/routing/main.go index 60af965a..ae081109 100644 --- a/_examples/routing/main.go +++ b/_examples/routing/main.go @@ -93,6 +93,8 @@ func registerSubdomains(app *iris.Application) { func newApp() *iris.Application { app := iris.New() + app.Logger().SetLevel("debug") + registerErrors(app) registerGamesRoutes(app) registerSubdomains(app) diff --git a/_examples/routing/overview/main.go b/_examples/routing/overview/main.go index 7fd55671..b9d5f106 100644 --- a/_examples/routing/overview/main.go +++ b/_examples/routing/overview/main.go @@ -6,7 +6,7 @@ import ( func main() { app := iris.New() - app.Logger().SetLevel("debug").SetTimeFormat("") + app.Logger().SetLevel("debug") // GET: http://localhost:8080 app.Get("/", info) diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 2b618447..8ed33a29 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -26,13 +26,13 @@ const MethodNone = "NONE" // "PATCH", "OPTIONS", "TRACE". var AllMethods = []string{ http.MethodGet, - http.MethodPost, - http.MethodPut, - http.MethodDelete, - http.MethodConnect, http.MethodHead, http.MethodPatch, + http.MethodPut, + http.MethodPost, + http.MethodDelete, http.MethodOptions, + http.MethodConnect, http.MethodTrace, } diff --git a/core/router/handler.go b/core/router/handler.go index 0bcd88b2..28774940 100644 --- a/core/router/handler.go +++ b/core/router/handler.go @@ -153,8 +153,33 @@ func (h *routerHandler) Build(provider RoutesProvider) error { continue } } + } - golog.Debugf(r.Trace()) // keep log different parameter types in the same path as different routes. + if golog.Default.Level == golog.DebugLevel { + // group routes by method and print them without the [DBUG] and time info, + // the route logs are colorful. + // Note: don't use map, we need to keep registered order, use + // different slices for each method. + collect := func(method string) (methodRoutes []*Route) { + for _, r := range registeredRoutes { + if r.Method == method { + methodRoutes = append(methodRoutes, r) + } + } + + return + } + + bckpTimeFormat := golog.Default.TimeFormat + defer golog.SetTimeFormat(bckpTimeFormat) + golog.SetTimeFormat("") + + for _, method := range AllMethods { + methodRoutes := collect(method) + for _, r := range methodRoutes { + golog.Println(r.Trace()) + } + } } return errgroup.Check(rp)