package server

import (
	"net/http"
	"time"

	"github.com/influxdata/chronograf"
)

// Logger is middleware that logs the request
func Logger(logger chronograf.Logger, next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		now := time.Now()
		logger.
			WithField("component", "server").
			WithField("remote_addr", r.RemoteAddr).
			WithField("method", r.Method).
			WithField("url", r.URL).
			Info("Request")
		next.ServeHTTP(w, r)
		later := time.Now()
		elapsed := later.Sub(now)

		logger.
			WithField("component", "server").
			WithField("remote_addr", r.RemoteAddr).
			WithField("response_time", elapsed.String()).
			Info("Success")
	}
	return http.HandlerFunc(fn)
}

package server

import (
	"net/http"
	"time"

	"github.com/influxdata/chronograf"
)

type logResponseWriter struct {
	http.ResponseWriter

	responseCode int
}

func (l *logResponseWriter) WriteHeader(status int) {
	l.responseCode = status
	l.ResponseWriter.WriteHeader(status)
}

// Logger is middleware that logs the request
func Logger(logger chronograf.Logger, next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		now := time.Now()
		logger.
			WithField("component", "server").
			WithField("remote_addr", r.RemoteAddr).
			WithField("method", r.Method).
			WithField("url", r.URL).
			Info("Request")

		lrr := &logResponseWriter{w, 0}
		next.ServeHTTP(lrr, r)
		later := time.Now()
		elapsed := later.Sub(now)

		logger.
			WithField("component", "server").
			WithField("remote_addr", r.RemoteAddr).
			WithField("response_time", elapsed.String()).
			WithField("code", lrr.responseCode).
			Info("Response: ", http.StatusText(lrr.responseCode))
	}
	return http.HandlerFunc(fn)
}

Add HTTP status code to logs
Add HTTP status code to logs

This adds the status code to the response log message to make it easier
to diagnose issues. It also replaces the placeholder "Success" message
with the decoded value of the HTTP Status, resulting in messages like:

INFO[0041] Response: Temporary Redirect                  code=307

...and so on. Both easily consumable by humans and machines.
