From b4a943dba1013ffa355fc2658a12b086579e1891 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 23 Jan 2019 15:19:02 +0100 Subject: [PATCH] Added helper function to get logger --- handler/create.go | 3 +-- handler/delete.go | 2 +- handler/helper.go | 7 ++++++- handler/read_all.go | 2 +- handler/read_one.go | 2 +- handler/update.go | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/handler/create.go b/handler/create.go index 1ab69e2..57c288b 100644 --- a/handler/create.go +++ b/handler/create.go @@ -18,7 +18,6 @@ package handler import ( "code.vikunja.io/web" "github.com/labstack/echo" - "github.com/op/go-logging" "net/http" ) @@ -46,7 +45,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error { // Check rights if !currentStruct.CanCreate(currentAuth) { - ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to create while not having the rights for it", currentAuth) + getLogger(ctx).Noticef("Tried to create while not having the rights for it", currentAuth) return echo.NewHTTPError(http.StatusForbidden) } diff --git a/handler/delete.go b/handler/delete.go index 9a4c49a..82a75f6 100644 --- a/handler/delete.go +++ b/handler/delete.go @@ -44,7 +44,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError) } if !currentStruct.CanDelete(currentAuth) { - ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to delete while not having the rights for it", currentAuth) + getLogger(ctx).Noticef("Tried to delete while not having the rights for it", currentAuth) return echo.NewHTTPError(http.StatusForbidden) } diff --git a/handler/helper.go b/handler/helper.go index 2e1bf92..be849f8 100644 --- a/handler/helper.go +++ b/handler/helper.go @@ -40,6 +40,11 @@ func HandleHTTPError(err error, ctx echo.Context) *echo.HTTPError { errDetails := a.HTTPError() return echo.NewHTTPError(errDetails.HTTPCode, errDetails) } - ctx.Get("LoggingProvider").(*logging.Logger).Error(err.Error()) + getLogger(ctx).Error(err.Error()) return echo.NewHTTPError(http.StatusInternalServerError) } + +// Helper function to get the logger +func getLogger(ctx echo.Context) *logging.Logger { + return ctx.Get("LoggingProvider").(*logging.Logger) +} diff --git a/handler/read_all.go b/handler/read_all.go index f09a130..9e678e2 100644 --- a/handler/read_all.go +++ b/handler/read_all.go @@ -46,7 +46,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { } pageNumber, err := strconv.Atoi(page) if err != nil { - ctx.Get("LoggingProvider").(*logging.Logger).Error(err.Error()) + getLogger(ctx).Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.") } if pageNumber < 0 { diff --git a/handler/read_one.go b/handler/read_one.go index 4bae48a..147e3e2 100644 --- a/handler/read_one.go +++ b/handler/read_one.go @@ -46,7 +46,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") } if !currentStruct.CanRead(currentAuth) { - ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to read one while not having the rights for it", currentAuth) + getLogger(ctx).Noticef("Tried to read one while not having the rights for it", currentAuth) return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this") } diff --git a/handler/update.go b/handler/update.go index 504c27e..2dc19dd 100644 --- a/handler/update.go +++ b/handler/update.go @@ -45,7 +45,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") } if !currentStruct.CanUpdate(currentAuth) { - ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to update while not having the rights for it", currentAuth) + getLogger(ctx).Noticef("Tried to update while not having the rights for it", currentAuth) return echo.NewHTTPError(http.StatusForbidden) }