route logging improvement: group by methods

Former-commit-id: ad884991433a244dc76bdad7314d98a5c204dac6
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-04-26 17:51:27 +03:00
parent 66e641513c
commit 346ca2a219
5 changed files with 34 additions and 7 deletions

View File

@ -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("<h1>Index Page</h1>")

View File

@ -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)

View File

@ -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)

View File

@ -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,
}

View File

@ -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)