@@ -160,8 +160,8 @@ type RequestLoggerConfig struct {
160160 LogReferer bool
161161 // LogUserAgent instructs logger to extract request user agent values.
162162 LogUserAgent bool
163- // LogStatus instructs logger to extract response status code. If handler chain returns an echo.HTTPError ,
164- // the status code is extracted from the echo.HTTPError returned
163+ // LogStatus instructs logger to extract response status code. If handler chain returns an error ,
164+ // the status code is extracted from the error satisfying echo.StatusCoder interface.
165165 LogStatus bool
166166 // LogContentLength instructs logger to extract content length header value. Note: this value could be different from
167167 // actual request body size as it could be spoofed etc.
@@ -211,7 +211,7 @@ type RequestLoggerValues struct {
211211 Referer string
212212 // UserAgent is request user agent values.
213213 UserAgent string
214- // Status is response status code. Then handler returns an echo.HTTPError then code from there .
214+ // Status is a response status code. When the handler returns an error satisfying echo.StatusCoder interface, then code from it .
215215 Status int
216216 // Error is error returned from executed handler chain.
217217 Error error
@@ -272,7 +272,6 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
272272 }
273273
274274 req := c .Request ()
275- res := c .Response ()
276275 start := now ()
277276
278277 if config .BeforeNextFunc != nil {
@@ -284,6 +283,7 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
284283 // checked with `c.Response().Committed` field.
285284 c .Echo ().HTTPErrorHandler (c , err )
286285 }
286+ res := c .Response ()
287287
288288 v := RequestLoggerValues {
289289 StartTime : start ,
@@ -330,26 +330,16 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
330330 v .UserAgent = req .UserAgent ()
331331 }
332332
333- var resp * echo.Response
334333 if config .LogStatus || config .LogResponseSize {
335- if r , err := echo .UnwrapResponse (res ); err != nil {
336- c .Logger ().Error ("can not determine response status and/or size. ResponseWriter in context does not implement unwrapper interface" )
337- } else {
338- resp = r
339- }
340- }
334+ resp , status := echo .ResolveResponseStatus (res , err )
341335
342- if config .LogStatus {
343- v .Status = - 1
344- if resp != nil {
345- v .Status = resp .Status
336+ if config .LogStatus {
337+ v .Status = status
346338 }
347- if err != nil && ! config .HandleError {
348- // this block should not be executed in case of HandleError=true as the global error handler will decide
349- // the status code. In that case status code could be different from what err contains.
350- var hsc echo.HTTPStatusCoder
351- if errors .As (err , & hsc ) {
352- v .Status = hsc .StatusCode ()
339+ if config .LogResponseSize {
340+ v .ResponseSize = - 1
341+ if resp != nil {
342+ v .ResponseSize = resp .Size
353343 }
354344 }
355345 }
@@ -359,12 +349,6 @@ func (config RequestLoggerConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
359349 if config .LogContentLength {
360350 v .ContentLength = req .Header .Get (echo .HeaderContentLength )
361351 }
362- if config .LogResponseSize {
363- v .ResponseSize = - 1
364- if resp != nil {
365- v .ResponseSize = resp .Size
366- }
367- }
368352 if logHeaders {
369353 v .Headers = map [string ][]string {}
370354 for _ , header := range headers {
0 commit comments