wip: custom logger and error handler

This commit is contained in:
2025-06-10 11:17:55 +01:00
parent 4935a65112
commit fae46f4bf7
7 changed files with 74 additions and 13 deletions
+28
View File
@@ -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
},
})
}