1
0

Improve logging handling (#57)

This commit is contained in:
konrad
2019-01-25 11:40:54 +00:00
committed by Gitea
parent d0fa9ddaec
commit 9e635ea54e
162 changed files with 29650 additions and 5877 deletions

View File

@ -22,6 +22,7 @@ implementations thus leading to much flexibility.
## Table of contents
* [Installation](#installation)
* [Todos](#todos)
* [CRUDable](#crudable)
* [Rights](#rights)
* [Handler Config](#handler-config)
@ -35,6 +36,12 @@ implementations thus leading to much flexibility.
* [Errors](#errors)
* [URL param binder](#how-the-url-param-binder-works)
### TODOs
* [ ] Description of web.HTTPError
* [ ] Rights methods should return errors (I know, this will break a lot of existing stuff)
* [ ] Improve docs
## Installation
Using the web handler in your application is pretty straight forward, simply run `go get -u code.vikunja.io/web` and start using it.

View File

@ -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 (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -18,7 +18,6 @@ package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"github.com/op/go-logging"
"net/http"
)
@ -44,7 +43,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 (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -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)
}

View File

@ -18,7 +18,6 @@ package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"github.com/op/go-logging"
"net/http"
"strconv"
)
@ -46,7 +45,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 {

View File

@ -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) 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 (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}

View File

@ -18,7 +18,6 @@ package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"github.com/op/go-logging"
"net/http"
)
@ -45,7 +44,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 (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}