File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export GO111MODULE=on
99dev-deps : # # Install Development dependencies
1010 npm i -g conventional-changelog-cli commitizen
1111
12- dev : # # Setup the Development envrionment
12+ dev : # # Setup the Development environment
1313 pre-commit install
1414
1515fmt : # # Formats the go code using gofmt
Original file line number Diff line number Diff line change @@ -9,10 +9,46 @@ import (
99 "go.uber.org/zap/zapcore"
1010)
1111
12+ type (
13+ Skipper func (c echo.Context ) bool
14+
15+ // ZapLoggerConfig defines the config for ZapLogger middleware
16+ ZapLoggerConfig struct {
17+ // Skipper defines a function to skip middleware
18+ Skipper Skipper
19+ }
20+ )
21+
22+ var (
23+ // DefaultZapLoggerConfig is the default ZapLogger middleware config.
24+ DefaultZapLoggerConfig = ZapLoggerConfig {
25+ Skipper : DefaultSkipper ,
26+ }
27+ )
28+
29+ // DefaultSkipper returns false which processes the middleware
30+ func DefaultSkipper (echo.Context ) bool {
31+ return false
32+ }
33+
1234// ZapLogger is a middleware and zap to provide an "access log" like logging for each request.
1335func ZapLogger (log * zap.Logger ) echo.MiddlewareFunc {
36+ return ZapLoggerWithConfig (log , DefaultZapLoggerConfig )
37+ }
38+
39+ // ZapLoggerWithConfig is a middleware (with configuration) and zap to provide an "access log" like logging for each request.
40+ func ZapLoggerWithConfig (log * zap.Logger , config ZapLoggerConfig ) echo.MiddlewareFunc {
1441 return func (next echo.HandlerFunc ) echo.HandlerFunc {
42+ // Defaults
43+ if config .Skipper == nil {
44+ config .Skipper = DefaultZapLoggerConfig .Skipper
45+ }
46+
1547 return func (c echo.Context ) error {
48+ if config .Skipper (c ) {
49+ return next (c )
50+ }
51+
1652 start := time .Now ()
1753
1854 err := next (c )
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package echozap
33import (
44 "net/http"
55 "net/http/httptest"
6+ "strings"
67 "testing"
78
89 "github.com/labstack/echo/v4"
@@ -38,3 +39,28 @@ func TestZapLogger(t *testing.T) {
3839 assert .NotNil (t , logFields ["host" ])
3940 assert .NotNil (t , logFields ["size" ])
4041}
42+
43+ func TestZapLoggerWithConfig (t * testing.T ) {
44+ e := echo .New ()
45+ req := httptest .NewRequest (http .MethodGet , "/something" , nil )
46+ rec := httptest .NewRecorder ()
47+ c := e .NewContext (req , rec )
48+
49+ h := func (c echo.Context ) error {
50+ return c .String (http .StatusOK , "" )
51+ }
52+
53+ obs , logs := observer .New (zap .DebugLevel )
54+
55+ logger := zap .New (obs )
56+
57+ err := ZapLoggerWithConfig (logger , ZapLoggerConfig {
58+ Skipper : func (ctx echo.Context ) bool {
59+ return strings .Contains (ctx .Request ().URL .Path , "/something" )
60+ },
61+ })(h )(c )
62+
63+ assert .Nil (t , err )
64+
65+ assert .Equal (t , 0 , logs .Len ())
66+ }
You can’t perform that action at this time.
0 commit comments