Skip to content

Commit

Permalink
Merge branches 'correct_logging' and 'master' of github.com:JSainsbur…
Browse files Browse the repository at this point in the history
…yPLC/xecho into correct_logging

 Conflicts:
	app.go
	logging.go
  • Loading branch information
KevinJCross committed Aug 6, 2020
1 parent 95fc386 commit 3cc7ba4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
9 changes: 7 additions & 2 deletions request_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xecho
import (
"math"
"net/http"
"strings"
"time"

"github.com/labstack/echo"
Expand All @@ -18,19 +19,23 @@ func RequestLoggerMiddleware(timeFn TimeProvider) echo.MiddlewareFunc {
}

func RequestLogger(c *Context, next echo.HandlerFunc, time TimeProvider) error {
request := c.Request()
if request.URL.Path == "/health" && strings.Contains(request.UserAgent(), "HealthChecker") {
return next(c)
}
before := time()
lrw := &statefulResponseWriter{ResponseWriter: c.Response().Writer}
c.Response().Writer = lrw
err := next(c)
after := time()
logger, ok := c.Logger().(*Logger)
if !ok {
c.Logger().Infof("[%s] %s %d", c.Request().Method, c.Path(), lrw.statusCode)
c.Logger().Infof("[%s] %s %d", request.Method, c.Path(), lrw.statusCode)
return err
}
logger.
WithFields(createMap(c, after.Sub(before), lrw, err)).
Infof("[%s] %s %d", c.Request().Method, c.Path(), lrw.statusCode)
Infof("[%s] %s %d", request.Method, c.Path(), lrw.statusCode)
return err
}

Expand Down
31 changes: 27 additions & 4 deletions request_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ func TestRequestLogger_LogTest(t *testing.T) {

}

func TestRequestLogger_HealthNoLogTest(t *testing.T) {
buffer := &bytes.Buffer{}
URL, _ := url.Parse("http://somedomain/health")
writer, _ := NewWriter()
context := createTestContext(writer, URL, buffer)
now := time.Now()
provider := testTimeProvider{calls: []time.Time{now, now.Add(755 * time.Millisecond)}}
nextCalled := false
nextPtr := &nextCalled
var next echo.HandlerFunc = func(context echo.Context) error {
*nextPtr = true
context.Response().WriteHeader(200)
return nil
}
err := RequestLoggerMiddleware(provider.Next)(next)(context)
assert.Nil(t, err)
fields := getLogFields(buffer, err, t)

assert.Equal(t, len(fields), 0)

}

func createTestContext(writer *responseWriter, URL *url.URL, buffer *bytes.Buffer) *Context {
return &Context{
Context: &testContext{
Expand All @@ -112,12 +134,11 @@ func createResponse(writer *responseWriter) *echo.Response {
Size: 150,
}
}

func createRequest(URL *url.URL) *http.Request {
return &http.Request{
Method: "GET",
URL: URL,
Header: http.Header{"Correlation-Id": []string{"set_one"}},
Header: http.Header{"Correlation-Id": []string{"set_one"}, "User-Agent": []string{"ELB-HealthChecker/2.0"}},
ContentLength: 34567,
Host: "this.is.a.domain",
RemoteAddr: "",
Expand All @@ -128,8 +149,10 @@ func createRequest(URL *url.URL) *http.Request {
func getLogFields(buffer *bytes.Buffer, err error, t *testing.T) logrus.Fields {
fields := logrus.Fields{}
logStatement := buffer.Bytes()
err = json.Unmarshal(logStatement, &fields)
assert.Nil(t, err)
if len(logStatement) > 0 {
err = json.Unmarshal(logStatement, &fields)
assert.Nil(t, err)
}
return fields
}

Expand Down

0 comments on commit 3cc7ba4

Please sign in to comment.