middleware is applied for all the path registered before and after `echo.Use`
Issue Description
Checklist
- [x] Dependencies installed
- [x] No typos
- [x] Searched existing issues and docs
Expected behaviour
According to the document https://echo.labstack.com/middleware
Middleware registed using Echo#Use() is only executed for paths which are registered after Echo#Use() has been called.
Actual behaviour
Middleware registed using Echo#Use() is executed for paths which are registered before and after Echo#Use() has been called.
I guess the document is not updated?
Steps to reproduce
Working code to debug
package main
import (
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "hello world")
})
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
e.GET("/log", func(c echo.Context) error {
return c.String(http.StatusOK, "logging")
})
e.Logger.Fatal(e.Start(":8000"))
}
Version/commit
v4.1.14
Have you found any workaround @kemingy? I've tried to play with groups without success. It's like if the root middlewares are always applied...
Have you found any workaround @kemingy? I've tried to play with groups without success. It's like if the root middlewares are always applied...
I guess currently the only way is using the middleware skipper.
Which is not available on every third-party middleware...I've finally moved all my stuff into one group (and apply middleware there) and left my health endpoints at root level.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
#1660 is still alive, so I'm reopening the issue
So I am getting 200 logs for my health check endpoints and I would prefer to either not see them at all or see them only if I set a log level of debug. What is the current solution to something like this?
https://github.com/labstack/echo/pull/1660 ended up getting closed.
Middleware skippers are the way to exclude certain paths to be excluded from middleware.
If you need to add skipper to some 3rd party middleware that does not have it, you can do this:
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
loggerMw := middleware.Logger() // to showcase some "3rd party" middleware
return func(c echo.Context) error {
if c.Path() == "/health" {
return next(c)
}
return loggerMw(next)(c)
}
})
p.s. Logger and RequestLogger middlewares have skipper functionality built in
Okay yeah I can just use this fn: https://pkg.go.dev/github.com/labstack/echo/[email protected]/middleware#LoggerWithConfig And then pass it:
LoggerConfig{
Skipper: func(c echo.Context) bool {
if c.Path() == "/health" {
return true
}
return false
}
}