Add logging across app and centralized error handling

This commit is contained in:
Andrey Chervyakov 2021-03-15 19:03:30 +06:00
parent 3bbeab1199
commit 4c3f095109
10 changed files with 115 additions and 62 deletions

34
middleware/logger.go Normal file
View file

@ -0,0 +1,34 @@
package middleware
import (
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"time"
)
func Logger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
start := time.Now()
if err = next(c); err != nil {
c.Error(err)
}
stop := time.Now()
log.Info().
Str("remote_ip", c.RealIP()).
Str("host", req.Host).
Str("uri", req.RequestURI).
Str("method", req.Method).
Str("path", req.URL.Path).
Str("user_agent", req.UserAgent()).
Int("status", res.Status).
Str("latency", stop.Sub(start).String()).
Send()
return
}
}
}