RequestLogger returns a logger handler using a custom LogFormatter.
(f LogFormatter)
| 42 | |
| 43 | // RequestLogger returns a logger handler using a custom LogFormatter. |
| 44 | func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler { |
| 45 | return func(next http.Handler) http.Handler { |
| 46 | fn := func(w http.ResponseWriter, r *http.Request) { |
| 47 | entry := f.NewLogEntry(r) |
| 48 | ww := NewWrapResponseWriter(w, r.ProtoMajor) |
| 49 | |
| 50 | t1 := time.Now() |
| 51 | defer func() { |
| 52 | entry.Write(ww.Status(), ww.BytesWritten(), ww.Header(), time.Since(t1), nil) |
| 53 | }() |
| 54 | |
| 55 | next.ServeHTTP(ww, WithLogEntry(r, entry)) |
| 56 | } |
| 57 | return http.HandlerFunc(fn) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // LogFormatter initiates the beginning of a new LogEntry per request. |
| 62 | // See DefaultLogFormatter for an example implementation. |
no test coverage detected