From d4a9ea4ba69d8395f924a54df33e8e81a2bde5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9rio=20Val=C3=A9rio?= Date: Fri, 26 May 2023 13:36:34 +0300 Subject: [PATCH] Add a Skipper middleware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the forked repo appeared to be unmaintained, we are pulling this open PR in: https://github.com/brpaz/echozap/pull/4 Signed-off-by: Valério Valério --- Makefile | 2 +- logger.go | 36 ++++++++++++++++++++++++++++++++++++ logger_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 953e319..b2534e4 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ export GO111MODULE=on dev-deps: ## Install Development dependencies npm i -g conventional-changelog-cli commitizen -dev: ## Setup the Development envrionment +dev: ## Setup the Development environment pre-commit install fmt: ## Formats the go code using gofmt diff --git a/logger.go b/logger.go index 7c10ed9..91f558f 100644 --- a/logger.go +++ b/logger.go @@ -9,10 +9,46 @@ import ( "go.uber.org/zap/zapcore" ) +type ( + Skipper func(c echo.Context) bool + + // ZapLoggerConfig defines the config for ZapLogger middleware + ZapLoggerConfig struct { + // Skipper defines a function to skip middleware + Skipper Skipper + } +) + +var ( + // DefaultZapLoggerConfig is the default ZapLogger middleware config. + DefaultZapLoggerConfig = ZapLoggerConfig{ + Skipper: DefaultSkipper, + } +) + +// DefaultSkipper returns false which processes the middleware +func DefaultSkipper(echo.Context) bool { + return false +} + // ZapLogger is a middleware and zap to provide an "access log" like logging for each request. func ZapLogger(log *zap.Logger) echo.MiddlewareFunc { + return ZapLoggerWithConfig(log, DefaultZapLoggerConfig) +} + +// ZapLoggerWithConfig is a middleware (with configuration) and zap to provide an "access log" like logging for each request. +func ZapLoggerWithConfig(log *zap.Logger, config ZapLoggerConfig) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { + // Defaults + if config.Skipper == nil { + config.Skipper = DefaultZapLoggerConfig.Skipper + } + return func(c echo.Context) error { + if config.Skipper(c) { + return next(c) + } + start := time.Now() err := next(c) diff --git a/logger_test.go b/logger_test.go index 0240ee1..ef085e0 100644 --- a/logger_test.go +++ b/logger_test.go @@ -3,6 +3,7 @@ package echozap import ( "net/http" "net/http/httptest" + "strings" "testing" "github.com/labstack/echo/v4" @@ -38,3 +39,28 @@ func TestZapLogger(t *testing.T) { assert.NotNil(t, logFields["host"]) assert.NotNil(t, logFields["size"]) } + +func TestZapLoggerWithConfig(t *testing.T) { + e := echo.New() + req := httptest.NewRequest(http.MethodGet, "/something", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + h := func(c echo.Context) error { + return c.String(http.StatusOK, "") + } + + obs, logs := observer.New(zap.DebugLevel) + + logger := zap.New(obs) + + err := ZapLoggerWithConfig(logger, ZapLoggerConfig{ + Skipper: func(ctx echo.Context) bool { + return strings.Contains(ctx.Request().URL.Path, "/something") + }, + })(h)(c) + + assert.Nil(t, err) + + assert.Equal(t, 0, logs.Len()) +}