Newer
Older
"github.com/kjk/betterguid"
)
type contextKey string
// Request wraps the given HTTP handler with a log entry that logs how long it has been running.
// It makes a preconfigured logger for the request available as part of the request context.
func Request(next http.Handler, logger log.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remote := r.Header.Get("X-Forwarded-For")
if remote == "" {
remote = r.RemoteAddr
}
// Try to figure out a name for the remote. If there are multiple, we select the first.
names, err := net.LookupAddr(remote)
if err != nil {
// Can't resolve name, but that's ok. There might not be a PTR record for the remote.
names = []string{"n/a"}
}
start := time.Now()
defer func() {
d := time.Since(start)
// Get returns the logger for the given HTTP request. It will return a logger that discards all writes if r does not have a logger in its context.
// GetContext returns the logger for the given context. If ctx has no logger, it returns
// a logger that will discard all logged events.
val := ctx.Value(contextKey("logger"))
if val == nil {
val = log.NewNopLogger()
}
return val.(log.Logger)