Skip to content

Commit

Permalink
route logging improvement: group by methods
Browse files Browse the repository at this point in the history
Former-commit-id: ad884991433a244dc76bdad7314d98a5c204dac6
  • Loading branch information
kataras committed Apr 26, 2020
1 parent 66e6415 commit 346ca2a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion _examples/mvc/grpc-compatible/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>")
Expand Down
2 changes: 2 additions & 0 deletions _examples/routing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _examples/routing/overview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions core/router/api_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
27 changes: 26 additions & 1 deletion core/router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,33 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
continue
}
}
}

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("")

golog.Debugf(r.Trace()) // keep log different parameter types in the same path as different routes.
for _, method := range AllMethods {
methodRoutes := collect(method)
for _, r := range methodRoutes {
golog.Println(r.Trace())
}
}
}

return errgroup.Check(rp)
Expand Down

0 comments on commit 346ca2a

Please sign in to comment.