diff --git a/handlers/requestlogger.go b/handlers/requestlogger.go new file mode 100644 index 0000000..273fa0b --- /dev/null +++ b/handlers/requestlogger.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "net/http" + + "time" + + "github.com/go-kit/kit/log" +) + +// NewRequestLogger logs each request with start and ending times +func NewRequestLogger(l log.Logger) func(h http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + l.Log("request", "start", "path", r.URL.Path, "remote_addr", r.RemoteAddr) + defer l.Log("request", "end", "duration", time.Since(start)) + + h.ServeHTTP(w, r) + }) + } +} diff --git a/main.go b/main.go index 53269dd..9b2e2d5 100644 --- a/main.go +++ b/main.go @@ -49,7 +49,6 @@ func main() { logger := log.With( log.NewLogfmtLogger(os.Stderr), "ts", log.DefaultTimestampUTC, - "caller", log.DefaultCaller, ) logger.Log( @@ -137,9 +136,10 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han decorators, // Checking on the route "health", doesn't support path-segment-stripping! - handlers.NewHTTPStatusPaths(l, []string{"health"}, http.StatusOK), + handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK), handlers.NewIgnoreFaviconRequests(), + handlers.NewRequestLogger(l), handlers.NewRateLimitHandler(l, b), )