Skip to content

Commit

Permalink
logger: allow routes to do not be logged by RequestLogger middleware (#4
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AndersonQ authored Jun 11, 2020
1 parent 2091dc7 commit c655b4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion logger/middleware/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Logger(log logger.Logger) func(http.Handler) http.Handler {
}
}

func RequestLogger() func(http.Handler) http.Handler {
func RequestLogger(skipRoutes []string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := logger.Now()
Expand Down Expand Up @@ -51,6 +51,12 @@ func RequestLogger() func(http.Handler) http.Handler {
ww := responseWriter{w: w, body: &bytes.Buffer{}}

defer func() {
for _, skipRoute := range skipRoutes {
if strings.HasPrefix(urlPath, skipRoute) {
// do not log this route
return
}
}
l.Info().
Str(internal.FieldEvent, internal.EventRequestFinished).
Int(internal.FieldStatus, ww.statusCode).
Expand Down
35 changes: 33 additions & 2 deletions logger/middleware/http_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func ExampleNewHttpHandlerLogger_simple() {
log := logger.New(os.Stdout, "")
ctx := log.WithContext(r.Context())

loggerMiddleware := RequestLogger()
loggerMiddleware := RequestLogger([]string{})

h := loggerMiddleware(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -82,7 +82,7 @@ func ExampleNewHttpHandlerLogger_complete() {
ctx = log.WithContext(ctx)

rr := r.WithContext(ctx)
loggerMiddleware := RequestLogger()
loggerMiddleware := RequestLogger([]string{})

h := loggerMiddleware(http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) { _, _ = w.Write(nil) }))
Expand All @@ -92,3 +92,34 @@ func ExampleNewHttpHandlerLogger_complete() {
// Output:
// {"level":"info","application":"","entry_point":true,"host":"example.com","ip":"42.42.42.42","params":"bar=foo","path":"/foo","request_depth":0,"request_id":"42","route":"","tree_path":"","user_agent":"","verb":"GET","event":"request_finished","status":200,"request_duration":1000,"timestamp":"2009-11-10T23:00:02Z","message":"GET /foo"}
}

func ExampleNewHttpHandlerLogger_skipRoutes() {
livePath := "/live"
// Set current time function so we can control the request duration
logger.SetNowFunc(func() time.Time {
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
})

w := httptest.NewRecorder()
rBar := httptest.NewRequest(http.MethodGet, "http://example.com/foo?bar=foo", nil)
rBar.Header.Set(internal.HeaderForwardedFor, "localhost")

rLive := httptest.NewRequest(http.MethodGet, "http://example.com"+livePath, nil)
rLive.Header.Set(internal.HeaderForwardedFor, "localhost")

log := logger.New(os.Stdout, "")
ctx := log.WithContext(rBar.Context())

loggerMiddleware := RequestLogger([]string{livePath})

h := loggerMiddleware(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "Hello, world")
}))

h.ServeHTTP(w, rBar.WithContext(ctx))
h.ServeHTTP(w, rLive.WithContext(ctx))

// Output:
// {"level":"info","application":"","entry_point":true,"host":"example.com","ip":"localhost","params":"bar=foo","path":"/foo","request_depth":0,"request_id":"","route":"","tree_path":"","user_agent":"","verb":"GET","event":"request_finished","status":200,"request_duration":0,"timestamp":"2009-11-10T23:00:00Z","message":"GET /foo"}
}

0 comments on commit c655b4f

Please sign in to comment.