Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce route Skipper #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}