Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 21 additions & 0 deletions echolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down