Skip to content

Commit

Permalink
Add a Skipper middleware
Browse files Browse the repository at this point in the history
Since the forked repo appeared to be unmaintained,
we are pulling this open PR in:
brpaz#4

Signed-off-by: Valério Valério <[email protected]>
  • Loading branch information
VDVsx committed May 26, 2023
1 parent 592e888 commit d4a9ea4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package echozap
import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -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())
}

0 comments on commit d4a9ea4

Please sign in to comment.