diff --git a/README.md b/README.md index 435e6bf..5d8d991 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,19 @@ func filter(c echo.Context) bool { func main() { e := echo.New() + // simple apply middleware without filter + e.Use(echolog.Default()) + // with filter + e.Use(echolog.Default("/healthcheck")) + // add more filter + e.Use(echolog.Default("/healthcheck", "foo", "bar")) + + // you can also do this e.Use(echolog.Middleware(filter)) + // or without filter + // e.Use(echolog.Middleware(nil)) // Output: {"level":"info","request_id":"9627a4a0-9d94-4ab6-844c-9599c0a15cd0","remote_ip":"[::1]:62542","host":"localhost:8080","method":"GET","path":"/","body":"","status_code":200,"latency":0,"tag":"request","time":"2023-02-19T14:07:37+07:00","message":"success"} - // Or without filter - // r.Use(echolog.Middleware(nil)) - e.GET("/", func(c echo.Context) error { ctx := c.Request().Context() log.Ctx(ctx).Info().Msg("hello world") diff --git a/echolog.go b/echolog.go index ccf3a99..3f6847c 100644 --- a/echolog.go +++ b/echolog.go @@ -143,6 +143,27 @@ func Middleware(filter func(c echo.Context) bool) echo.MiddlewareFunc { } } +// Default contains functionality to filter Request URI with paramater type of string +func Default(filters ...string) echo.MiddlewareFunc { + if len(filters) > 0 { + return Middleware(func(c echo.Context) bool { + return filtered(c, filters) + }) + } + + return Middleware(nil) +} + +func filtered(c echo.Context, filters []string) bool { + for _, filter := range filters { + if c.Request().RequestURI == filter { + return true + } + } + + return false +} + func formatReqBody(data []byte) string { var js map[string]interface{} if json.Unmarshal(data, &js) != nil {