mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
wip: custom logger and error handler
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo-contrib/session"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
@@ -43,3 +46,28 @@ func (api *API) AuthSessionMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return next(context)
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) LoggerMiddleware() echo.MiddlewareFunc {
|
||||
return middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
||||
LogStatus: true,
|
||||
LogURI: true,
|
||||
LogError: true,
|
||||
HandleError: true,
|
||||
LogMethod: true,
|
||||
LogValuesFunc: func(logContext echo.Context, values middleware.RequestLoggerValues) error {
|
||||
if values.Error == nil {
|
||||
api.Logger.LogAttrs(context.Background(), slog.LevelInfo, "REQUEST",
|
||||
slog.String("uri", values.URI),
|
||||
slog.Int("status", values.Status),
|
||||
)
|
||||
} else {
|
||||
api.Logger.LogAttrs(context.Background(), slog.LevelError, "REQUEST_ERROR",
|
||||
slog.String("uri", values.URI),
|
||||
slog.Int("status", values.Status),
|
||||
slog.String("err", values.Error.Error()),
|
||||
)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user