1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddyhttp: Better duration logging

Also un-nest all the error handling, that was unnecessary indentation
This commit is contained in:
Matthew Holt 2020-04-28 15:38:45 -06:00
parent 10db57027d
commit f931c26f68
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -153,9 +153,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// to capture the original request in case it gets // to capture the original request in case it gets
// modified during handling // modified during handling
loggableReq := zap.Object("request", LoggableHTTPRequest{r}) loggableReq := zap.Object("request", LoggableHTTPRequest{r})
errLog := s.errorLogger.With( errLog := s.errorLogger.With(loggableReq)
loggableReq,
) var duration time.Duration
if s.shouldLogRequest(r) { if s.shouldLogRequest(r) {
wrec := NewResponseRecorder(w, nil, nil) wrec := NewResponseRecorder(w, nil, nil)
@ -164,13 +164,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// capture the original version of the request // capture the original version of the request
accLog := s.accessLogger.With(loggableReq) accLog := s.accessLogger.With(loggableReq)
start := time.Now()
defer func() { defer func() {
latency := time.Since(start)
repl.Set("http.response.status", wrec.Status()) repl.Set("http.response.status", wrec.Status())
repl.Set("http.response.size", wrec.Size()) repl.Set("http.response.size", wrec.Size())
repl.Set("http.response.latency", latency) repl.Set("http.response.duration", duration)
logger := accLog logger := accLog
if s.Logs != nil { if s.Logs != nil {
@ -184,7 +181,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log("handled request", log("handled request",
zap.String("common_log", repl.ReplaceAll(commonLogFormat, commonLogEmptyValue)), zap.String("common_log", repl.ReplaceAll(commonLogFormat, commonLogEmptyValue)),
zap.Duration("latency", latency), zap.Duration("duration", duration),
zap.Int("size", wrec.Size()), zap.Int("size", wrec.Size()),
zap.Int("status", wrec.Status()), zap.Int("status", wrec.Status()),
zap.Object("resp_headers", LoggableHTTPHeader(wrec.Header())), zap.Object("resp_headers", LoggableHTTPHeader(wrec.Header())),
@ -192,51 +189,60 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}() }()
} }
start := time.Now()
// guarantee ACME HTTP challenges; handle them // guarantee ACME HTTP challenges; handle them
// separately from any user-defined handlers // separately from any user-defined handlers
if s.tlsApp.HandleHTTPChallenge(w, r) { if s.tlsApp.HandleHTTPChallenge(w, r) {
duration = time.Since(start)
return return
} }
// execute the primary handler chain // execute the primary handler chain
err := s.primaryHandlerChain.ServeHTTP(w, r) err := s.primaryHandlerChain.ServeHTTP(w, r)
if err != nil { duration = time.Since(start)
// prepare the error log
logger := errLog
if s.Logs != nil {
logger = s.Logs.wrapLogger(logger, r.Host)
}
// get the values that will be used to log the error // if no errors, we're done!
errStatus, errMsg, errFields := errLogValues(err) if err == nil {
return
}
// add HTTP error information to request context // prepare the error log
r = s.Errors.WithError(r, err) logger := errLog
if s.Logs != nil {
logger = s.Logs.wrapLogger(logger, r.Host)
}
logger = logger.With(zap.Duration("duration", duration))
if s.Errors != nil && len(s.Errors.Routes) > 0 { // get the values that will be used to log the error
// execute user-defined error handling route errStatus, errMsg, errFields := errLogValues(err)
err2 := s.errorHandlerChain.ServeHTTP(w, r)
if err2 == nil { // add HTTP error information to request context
// user's error route handled the error response r = s.Errors.WithError(r, err)
// successfully, so now just log the error
if errStatus >= 500 { if s.Errors != nil && len(s.Errors.Routes) > 0 {
logger.Error(errMsg, errFields...) // execute user-defined error handling route
} err2 := s.errorHandlerChain.ServeHTTP(w, r)
} else { if err2 == nil {
// well... this is awkward // user's error route handled the error response
errFields = append([]zapcore.Field{ // successfully, so now just log the error
zap.String("error", err2.Error()),
zap.Namespace("first_error"),
zap.String("msg", errMsg),
}, errFields...)
logger.Error("error handling handler error", errFields...)
}
} else {
if errStatus >= 500 { if errStatus >= 500 {
logger.Error(errMsg, errFields...) logger.Error(errMsg, errFields...)
} }
w.WriteHeader(errStatus) } else {
// well... this is awkward
errFields = append([]zapcore.Field{
zap.String("error", err2.Error()),
zap.Namespace("first_error"),
zap.String("msg", errMsg),
}, errFields...)
logger.Error("error handling handler error", errFields...)
} }
} else {
if errStatus >= 500 {
logger.Error(errMsg, errFields...)
}
w.WriteHeader(errStatus)
} }
} }