From 6fb314b326d530322f1a2e674f250a265268082c Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 1 Sep 2024 19:30:09 +0200 Subject: [PATCH] chore(web): use logger directly --- pkg/routes/routes.go | 3 --- pkg/web/handler/config.go | 37 ------------------------------------- pkg/web/handler/create.go | 7 ++++--- pkg/web/handler/delete.go | 7 ++++--- pkg/web/handler/helper.go | 4 +++- pkg/web/handler/read_all.go | 9 +++++---- pkg/web/handler/read_one.go | 7 ++++--- pkg/web/handler/update.go | 7 ++++--- 8 files changed, 24 insertions(+), 57 deletions(-) delete mode 100644 pkg/web/handler/config.go diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index 86b336ccb..ccff261c6 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -116,9 +116,6 @@ func NewEcho() *echo.Echo { // Validation e.Validator = &CustomValidator{} - // Handler config - handler.SetLoggingProvider(log.GetLogger()) - return e } diff --git a/pkg/web/handler/config.go b/pkg/web/handler/config.go deleted file mode 100644 index e84448b28..000000000 --- a/pkg/web/handler/config.go +++ /dev/null @@ -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 . - -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 -} diff --git a/pkg/web/handler/create.go b/pkg/web/handler/create.go index fc12e84d9..0932be2a9 100644 --- a/pkg/web/handler/create.go +++ b/pkg/web/handler/create.go @@ -22,6 +22,7 @@ import ( "net/http" "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/auth" "github.com/labstack/echo/v4" @@ -34,7 +35,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error { // Get the object & bind params to struct 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 if errors.As(err, &he) { 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() { err = s.Close() 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 { _ = 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) } diff --git a/pkg/web/handler/delete.go b/pkg/web/handler/delete.go index c6e8e7525..599c81cf9 100644 --- a/pkg/web/handler/delete.go +++ b/pkg/web/handler/delete.go @@ -22,6 +22,7 @@ import ( "net/http" "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/auth" "github.com/labstack/echo/v4" @@ -39,7 +40,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error { // Bind params to struct 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 if errors.As(err, &he) { 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() { err = s.Close() 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 { _ = 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) } diff --git a/pkg/web/handler/helper.go b/pkg/web/handler/helper.go index 22328f67c..ed24057d6 100644 --- a/pkg/web/handler/helper.go +++ b/pkg/web/handler/helper.go @@ -19,7 +19,9 @@ package handler import ( "net/http" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/web" + "github.com/labstack/echo/v4" ) @@ -37,7 +39,7 @@ type CObject interface { // HandleHTTPError does what it says func HandleHTTPError(err error) *echo.HTTPError { - config.LoggingProvider.Error(err.Error()) + log.Error(err.Error()) if a, has := err.(web.HTTPErrorProcessor); has { errDetails := a.HTTPError() return echo.NewHTTPError(errDetails.HTTPCode, errDetails) diff --git a/pkg/web/handler/read_all.go b/pkg/web/handler/read_all.go index f41dd82ce..dae6b298b 100644 --- a/pkg/web/handler/read_all.go +++ b/pkg/web/handler/read_all.go @@ -25,6 +25,7 @@ import ( vconfig "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/auth" "github.com/labstack/echo/v4" @@ -42,7 +43,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { // Get the object & bind params to struct 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 if errors.As(err, &he) { 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) if err != nil { - config.LoggingProvider.Error(err.Error()) + log.Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.") } if pageNumber < 0 { @@ -72,7 +73,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { if perPage != "" { perPageNumber, err = strconv.Atoi(perPage) if err != nil { - config.LoggingProvider.Error(err.Error()) + log.Error(err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Bad per page amount requested.") } } @@ -92,7 +93,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { defer func() { err = s.Close() if err != nil { - config.LoggingProvider.Errorf("Could not close session: %s", err) + log.Errorf("Could not close session: %s", err) } }() diff --git a/pkg/web/handler/read_one.go b/pkg/web/handler/read_one.go index 113da9f68..1b742a580 100644 --- a/pkg/web/handler/read_one.go +++ b/pkg/web/handler/read_one.go @@ -23,6 +23,7 @@ import ( "strconv" "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/auth" "github.com/labstack/echo/v4" @@ -35,7 +36,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error { // Get the object & bind params to struct 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 if errors.As(err, &he) { 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() { err = s.Close() 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 { _ = 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") } diff --git a/pkg/web/handler/update.go b/pkg/web/handler/update.go index f6339c4b4..3e58d97c3 100644 --- a/pkg/web/handler/update.go +++ b/pkg/web/handler/update.go @@ -22,6 +22,7 @@ import ( "net/http" "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/auth" "github.com/labstack/echo/v4" @@ -35,7 +36,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error { // Get the object & bind params to struct 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 if errors.As(err, &he) { 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() { err = s.Close() 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 { _ = 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) }