1
0

chore(web): use logger directly

(cherry picked from commit 6fb314b326d530322f1a2e674f250a265268082c)
This commit is contained in:
kolaente 2024-09-01 19:30:09 +02:00
parent fe44b7d473
commit a2ef74cade
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
8 changed files with 24 additions and 57 deletions

View File

@ -116,9 +116,6 @@ func NewEcho() *echo.Echo {
// Validation // Validation
e.Validator = &CustomValidator{} e.Validator = &CustomValidator{}
// Handler config
handler.SetLoggingProvider(log.GetLogger())
return e return e
} }

View File

@ -1,37 +0,0 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"github.com/op/go-logging"
)
// Config contains the config for the web handler
type Config struct {
LoggingProvider *logging.Logger
}
var config *Config
func init() {
config = &Config{}
}
// SetLoggingProvider sets the logging provider in the config
func SetLoggingProvider(logger *logging.Logger) {
config.LoggingProvider = logger
}

View File

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -34,7 +35,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
// Get the object & bind params to struct // Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil { if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error()) log.Debugf("Invalid model error. Internal error was: %s", err.Error())
var he *echo.HTTPError var he *echo.HTTPError
if errors.As(err, &he) { if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
@ -58,7 +59,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
defer func() { defer func() {
err = s.Close() err = s.Close()
if err != nil { if err != nil {
config.LoggingProvider.Errorf("Could not close session: %s", err) log.Errorf("Could not close session: %s", err)
} }
}() }()
@ -70,7 +71,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
} }
if !canCreate { if !canCreate {
_ = s.Rollback() _ = s.Rollback()
config.LoggingProvider.Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth) log.Warningf("Tried to create while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden) return echo.NewHTTPError(http.StatusForbidden)
} }

View File

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -39,7 +40,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
// Bind params to struct // Bind params to struct
if err := ctx.Bind(currentStruct); err != nil { if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error()) log.Debugf("Invalid model error. Internal error was: %s", err.Error())
var he *echo.HTTPError var he *echo.HTTPError
if errors.As(err, &he) { if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
@ -58,7 +59,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
defer func() { defer func() {
err = s.Close() err = s.Close()
if err != nil { if err != nil {
config.LoggingProvider.Errorf("Could not close session: %s", err) log.Errorf("Could not close session: %s", err)
} }
}() }()
@ -69,7 +70,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
} }
if !canDelete { if !canDelete {
_ = s.Rollback() _ = s.Rollback()
config.LoggingProvider.Noticef("Tried to delete while not having the rights for it (User: %v)", currentAuth) log.Warningf("Tried to delete while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden) return echo.NewHTTPError(http.StatusForbidden)
} }

View File

@ -19,7 +19,9 @@ package handler
import ( import (
"net/http" "net/http"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/web" "code.vikunja.io/api/pkg/web"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -37,7 +39,7 @@ type CObject interface {
// HandleHTTPError does what it says // HandleHTTPError does what it says
func HandleHTTPError(err error) *echo.HTTPError { func HandleHTTPError(err error) *echo.HTTPError {
config.LoggingProvider.Error(err.Error()) log.Error(err.Error())
if a, has := err.(web.HTTPErrorProcessor); has { if a, has := err.(web.HTTPErrorProcessor); has {
errDetails := a.HTTPError() errDetails := a.HTTPError()
return echo.NewHTTPError(errDetails.HTTPCode, errDetails) return echo.NewHTTPError(errDetails.HTTPCode, errDetails)

View File

@ -25,6 +25,7 @@ import (
vconfig "code.vikunja.io/api/pkg/config" vconfig "code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -42,7 +43,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
// Get the object & bind params to struct // Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil { if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error()) log.Debugf("Invalid model error. Internal error was: %s", err.Error())
var he *echo.HTTPError var he *echo.HTTPError
if errors.As(err, &he) { if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
@ -57,7 +58,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
} }
pageNumber, err := strconv.Atoi(page) pageNumber, err := strconv.Atoi(page)
if err != nil { if err != nil {
config.LoggingProvider.Error(err.Error()) log.Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.") return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.")
} }
if pageNumber < 0 { if pageNumber < 0 {
@ -72,7 +73,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
if perPage != "" { if perPage != "" {
perPageNumber, err = strconv.Atoi(perPage) perPageNumber, err = strconv.Atoi(perPage)
if err != nil { if err != nil {
config.LoggingProvider.Error(err.Error()) log.Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Bad per page amount requested.") return echo.NewHTTPError(http.StatusBadRequest, "Bad per page amount requested.")
} }
} }
@ -92,7 +93,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
defer func() { defer func() {
err = s.Close() err = s.Close()
if err != nil { if err != nil {
config.LoggingProvider.Errorf("Could not close session: %s", err) log.Errorf("Could not close session: %s", err)
} }
}() }()

View File

@ -23,6 +23,7 @@ import (
"strconv" "strconv"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -35,7 +36,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Get the object & bind params to struct // Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil { if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error()) log.Debugf("Invalid model error. Internal error was: %s", err.Error())
var he *echo.HTTPError var he *echo.HTTPError
if errors.As(err, &he) { if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
@ -54,7 +55,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
defer func() { defer func() {
err = s.Close() err = s.Close()
if err != nil { if err != nil {
config.LoggingProvider.Errorf("Could not close session: %s", err) log.Errorf("Could not close session: %s", err)
} }
}() }()
@ -65,7 +66,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
} }
if !canRead { if !canRead {
_ = s.Rollback() _ = s.Rollback()
config.LoggingProvider.Noticef("Tried to read while not having the rights for it (User: %v)", currentAuth) log.Warningf("Tried to read while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this") return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
} }

View File

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth" "code.vikunja.io/api/pkg/modules/auth"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -35,7 +36,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
// Get the object & bind params to struct // Get the object & bind params to struct
if err := ctx.Bind(currentStruct); err != nil { if err := ctx.Bind(currentStruct); err != nil {
config.LoggingProvider.Debugf("Invalid model error. Internal error was: %s", err.Error()) log.Debugf("Invalid model error. Internal error was: %s", err.Error())
var he *echo.HTTPError var he *echo.HTTPError
if errors.As(err, &he) { if errors.As(err, &he) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid model provided. Error was: %s", he.Message))
@ -59,7 +60,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
defer func() { defer func() {
err = s.Close() err = s.Close()
if err != nil { if err != nil {
config.LoggingProvider.Errorf("Could not close session: %s", err) log.Errorf("Could not close session: %s", err)
} }
}() }()
@ -70,7 +71,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
} }
if !canUpdate { if !canUpdate {
_ = s.Rollback() _ = s.Rollback()
config.LoggingProvider.Noticef("Tried to update while not having the rights for it (User: %v)", currentAuth) log.Warningf("Tried to update while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden) return echo.NewHTTPError(http.StatusForbidden)
} }