@marcel91200 / caddy Mirror · read-only / commits / f931c26f
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: Better duration logging

Also un-nest all the error handling, that was unnecessary indentation

Matthew Holt committed Apr 28, 2020 at 15:38 UTC f931c26f68030861df340c65c2ebd835f1eda11e
1 file changed +46 -40
modules/caddyhttp/server.go
+46 -40
@@ -153,9 +153,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
153 // to capture the original request in case it gets
154 // modified during handling
155 loggableReq := zap.Object("request", LoggableHTTPRequest{r})
156 - errLog := s.errorLogger.With(
157 - loggableReq,
158 - )
156 + errLog := s.errorLogger.With(loggableReq)
157 +
158 + var duration time.Duration
159
160 if s.shouldLogRequest(r) {
161 wrec := NewResponseRecorder(w, nil, nil)
@@ -164,13 +164,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
164 // capture the original version of the request
165 accLog := s.accessLogger.With(loggableReq)
166
167 - start := time.Now()
167 defer func() {
169 - latency := time.Since(start)
170 -
168 repl.Set("http.response.status", wrec.Status())
169 repl.Set("http.response.size", wrec.Size())
173 - repl.Set("http.response.latency", latency)
170 + repl.Set("http.response.duration", duration)
171
172 logger := accLog
173 if s.Logs != nil {
@@ -184,7 +181,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
181
182 log("handled request",
183 zap.String("common_log", repl.ReplaceAll(commonLogFormat, commonLogEmptyValue)),
187 - zap.Duration("latency", latency),
184 + zap.Duration("duration", duration),
185 zap.Int("size", wrec.Size()),
186 zap.Int("status", wrec.Status()),
187 zap.Object("resp_headers", LoggableHTTPHeader(wrec.Header())),
@@ -192,51 +189,60 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
189 }()
190 }
191
192 + start := time.Now()
193 +
194 // guarantee ACME HTTP challenges; handle them
195 // separately from any user-defined handlers
196 if s.tlsApp.HandleHTTPChallenge(w, r) {
197 + duration = time.Since(start)
198 return
199 }
200
201 // execute the primary handler chain
202 err := s.primaryHandlerChain.ServeHTTP(w, r)
203 - if err != nil {
204 - // prepare the error log
205 - logger := errLog
206 - if s.Logs != nil {
207 - logger = s.Logs.wrapLogger(logger, r.Host)
208 - }
203 + duration = time.Since(start)
204
210 - // get the values that will be used to log the error
211 - errStatus, errMsg, errFields := errLogValues(err)
212 -
213 - // add HTTP error information to request context
214 - r = s.Errors.WithError(r, err)
215 -
216 - if s.Errors != nil && len(s.Errors.Routes) > 0 {
217 - // execute user-defined error handling route
218 - err2 := s.errorHandlerChain.ServeHTTP(w, r)
219 - if err2 == nil {
220 - // user's error route handled the error response
221 - // successfully, so now just log the error
222 - if errStatus >= 500 {
223 - logger.Error(errMsg, errFields...)
224 - }
225 - } else {
226 - // well... this is awkward
227 - errFields = append([]zapcore.Field{
228 - zap.String("error", err2.Error()),
229 - zap.Namespace("first_error"),
230 - zap.String("msg", errMsg),
231 - }, errFields...)
232 - logger.Error("error handling handler error", errFields...)
233 - }
234 - } else {
205 + // if no errors, we're done!
206 + if err == nil {
207 + return
208 + }
209 +
210 + // prepare the error log
211 + logger := errLog
212 + if s.Logs != nil {
213 + logger = s.Logs.wrapLogger(logger, r.Host)
214 + }
215 + logger = logger.With(zap.Duration("duration", duration))
216 +
217 + // get the values that will be used to log the error
218 + errStatus, errMsg, errFields := errLogValues(err)
219 +
220 + // add HTTP error information to request context
221 + r = s.Errors.WithError(r, err)
222 +
223 + if s.Errors != nil && len(s.Errors.Routes) > 0 {
224 + // execute user-defined error handling route
225 + err2 := s.errorHandlerChain.ServeHTTP(w, r)
226 + if err2 == nil {
227 + // user's error route handled the error response
228 + // successfully, so now just log the error
229 if errStatus >= 500 {
230 logger.Error(errMsg, errFields...)
231 }
238 - w.WriteHeader(errStatus)
232 + } else {
233 + // well... this is awkward
234 + errFields = append([]zapcore.Field{
235 + zap.String("error", err2.Error()),
236 + zap.Namespace("first_error"),
237 + zap.String("msg", errMsg),
238 + }, errFields...)
239 + logger.Error("error handling handler error", errFields...)
240 + }
241 + } else {
242 + if errStatus >= 500 {
243 + logger.Error(errMsg, errFields...)
244 }
245 + w.WriteHeader(errStatus)
246 }
247 }
248