tsweb: add a StdHandler that doesn't log 200 responses.

Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
David Anderson 2020-04-05 00:14:22 -07:00
parent ba2774ea27
commit af3a9dfad6
1 changed files with 15 additions and 4 deletions

View File

@ -170,11 +170,19 @@ func (h HandlerFunc) ServeHTTPErr(w http.ResponseWriter, r *http.Request) error
// Handled requests are logged using logf, as are any errors. Errors // Handled requests are logged using logf, as are any errors. Errors
// are handled as specified by the Handler interface. // are handled as specified by the Handler interface.
func StdHandler(h Handler, logf logger.Logf) http.Handler { func StdHandler(h Handler, logf logger.Logf) http.Handler {
return stdHandler(h, logf, time.Now) return stdHandler(h, logf, time.Now, true)
} }
func stdHandler(h Handler, logf logger.Logf, now func() time.Time) http.Handler { // StdHandlerNo200s is like StdHandler, but successfully handled HTTP
return handler{h, logf, now} // requests don't write an access log entry to logf.
//
// TODO(danderson): quick stopgap, probably want ...Options on StdHandler instead?
func StdHandlerNo200s(h Handler, logf logger.Logf) http.Handler {
return stdHandler(h, logf, time.Now, false)
}
func stdHandler(h Handler, logf logger.Logf, now func() time.Time, log200s bool) http.Handler {
return handler{h, logf, now, log200s}
} }
// handler is an http.Handler that wraps a Handler and handles errors. // handler is an http.Handler that wraps a Handler and handles errors.
@ -182,6 +190,7 @@ type handler struct {
h Handler h Handler
logf logger.Logf logf logger.Logf
timeNow func() time.Time timeNow func() time.Time
log200s bool
} }
// ServeHTTP implements the http.Handler interface. // ServeHTTP implements the http.Handler interface.
@ -248,7 +257,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
h.logf("%s", msg) if msg.Code != 200 || h.log200s {
h.logf("%s", msg)
}
} }
// loggingResponseWriter wraps a ResponseWriter and record the HTTP // loggingResponseWriter wraps a ResponseWriter and record the HTTP