Open platform to collect and prioritize feedback
enhancement: add response status code to logs (#817)
Guilherme Oenningbf19cf041069
diff --git a/app/middlewares/setup.go b/app/middlewares/setup.goindex 79bd66a77..ad90a6474 100644--- a/app/middlewares/setup.go+++ b/app/middlewares/setup.go@@ -92,8 +92,9 @@ func WebSetup() web.MiddlewareFunc {trx, err := dbx.BeginTx(c)if err != nil {err = c.Failure(err)- log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms", dto.Props{+ log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms", dto.Props{"HttpMethod": c.Request.Method,+ "StatusCode": c.ResponseStatusCode,"URL": c.Request.URL.String(),"ElapsedMs": time.Since(c.Request.StartTime).Nanoseconds() / int64(time.Millisecond),})@@ -105,8 +106,9 @@ func WebSetup() web.MiddlewareFunc {//Execute the chainif err := next(c); err != nil {c.Rollback()- log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", dto.Props{+ log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms (rolled back)", dto.Props{"HttpMethod": c.Request.Method,+ "StatusCode": c.ResponseStatusCode,"URL": c.Request.URL.String(),"ElapsedMs": time.Since(c.Request.StartTime).Nanoseconds() / int64(time.Millisecond),})@@ -118,8 +120,9 @@ func WebSetup() web.MiddlewareFunc {log.Errorf(c, "Failed to commit request with: @{Error}", dto.Props{"Error": err.Error(),})- log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (rolled back)", dto.Props{+ log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms (rolled back)", dto.Props{"HttpMethod": c.Request.Method,+ "StatusCode": c.ResponseStatusCode,"URL": c.Request.URL.String(),"ElapsedMs": time.Since(c.Request.StartTime).Nanoseconds() / int64(time.Millisecond),})@@ -127,8 +130,9 @@ func WebSetup() web.MiddlewareFunc {}//Still no errors, everything is fine!- log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished in @{ElapsedMs:magenta}ms (committed)", dto.Props{+ log.Infof(c, "@{HttpMethod:magenta} @{URL:magenta} finished with @{StatusCode:magenta} in @{ElapsedMs:magenta}ms (committed)", dto.Props{"HttpMethod": c.Request.Method,+ "StatusCode": c.ResponseStatusCode,"URL": c.Request.URL.String(),"ElapsedMs": time.Since(c.Request.StartTime).Nanoseconds() / int64(time.Millisecond),})diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 292f21aca..014c3db96 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -68,13 +68,14 @@ const CookieSignUpAuthName = "__signup_auth"//Context shared between http pipelinetype Context struct {context.Context- Response http.ResponseWriter- Request Request- id string- sessionID string- engine *Engine- params StringMap- tasks []worker.Task+ Response http.ResponseWriter+ Request Request+ ResponseStatusCode int+ id string+ sessionID string+ engine *Engine+ params StringMap+ tasks []worker.Task}//NewContext creates a new web Context@@ -473,6 +474,7 @@ func (c *Context) Image(contentType string, b []byte) error {// Blob sends a blob response with status code and content type.func (c *Context) Blob(code int, contentType string, b []byte) error {c.Response.Header().Set("Content-Type", contentType)+ c.ResponseStatusCode = codec.Response.WriteHeader(code)_, err := c.Response.Write(b)return err@@ -480,6 +482,7 @@ func (c *Context) Blob(code int, contentType string, b []byte) error {// NoContent sends a response with no body and a status code.func (c *Context) NoContent(code int) error {+ c.ResponseStatusCode = codec.Response.WriteHeader(code)return nil}@@ -488,6 +491,7 @@ func (c *Context) NoContent(code int) error {func (c *Context) Redirect(url string) error {c.Response.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")c.Response.Header().Set("Location", url)+ c.ResponseStatusCode = http.StatusTemporaryRedirectc.Response.WriteHeader(http.StatusTemporaryRedirect)return nil}@@ -496,6 +500,7 @@ func (c *Context) Redirect(url string) error {func (c *Context) PermanentRedirect(url string) error {c.Response.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")c.Response.Header().Set("Location", url)+ c.ResponseStatusCode = http.StatusMovedPermanentlyc.Response.WriteHeader(http.StatusMovedPermanently)return nil}diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.goindex 20792ba06..36dc40ee4 100644--- a/app/pkg/web/context_test.go+++ b/app/pkg/web/context_test.go@@ -232,6 +232,7 @@ func TestTryAgainLater(t *testing.T) {err := ctx.TryAgainLater(24 * time.Hour)Expect(err).IsNil()resp := ctx.Response.(*httptest.ResponseRecorder)+ Expect(ctx.ResponseStatusCode).Equals(http.StatusServiceUnavailable)Expect(resp.Code).Equals(http.StatusServiceUnavailable)Expect(resp.Header().Get("Cache-Control")).Equals("no-cache, no-store, must-revalidate")Expect(resp.Header().Get("Retry-After")).Equals("86400")