Skip to content

Commit

Permalink
Adding IP Addresses in log entries
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
Ben Huson authored and vishr committed Sep 18, 2015
1 parent 27cedaf commit 799f01a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ const (
Upgrade = "Upgrade"
Vary = "Vary"
WWWAuthenticate = "WWW-Authenticate"

XForwardedFor = "X-Forwarded-For"
XRealIP = "X-Real-IP"
//-----------
// Protocols
//-----------
Expand Down
22 changes: 17 additions & 5 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"log"
"net"
"time"

"github.com/labstack/echo"
Expand All @@ -11,19 +12,30 @@ import (
func Logger() echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
req := c.Request()
res := c.Response()

remoteAddr := req.RemoteAddr
if ip := req.Header.Get(echo.XRealIP); ip != "" {
remoteAddr = ip
} else if ip = req.Header.Get(echo.XForwardedFor); ip != "" {
remoteAddr = ip
}
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)

start := time.Now()
if err := h(c); err != nil {
c.Error(err)
}
stop := time.Now()
method := c.Request().Method
path := c.Request().URL.Path
method := req.Method
path := req.URL.Path
if path == "" {
path = "/"
}
size := c.Response().Size()
size := res.Size()

n := c.Response().Status()
n := res.Status()
code := color.Green(n)
switch {
case n >= 500:
Expand All @@ -34,7 +46,7 @@ func Logger() echo.MiddlewareFunc {
code = color.Cyan(n)
}

log.Printf("%s %s %s %s %d", method, path, code, stop.Sub(start), size)
log.Printf("%s %s %s %s %s %d", remoteAddr, method, path, code, stop.Sub(start), size)
return nil
}
}
Expand Down
21 changes: 19 additions & 2 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@ package middleware

import (
"errors"
"github.com/labstack/echo"
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo"
)

func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), e)

// Status 2xx
// With X-Real-IP
req.Header.Add(echo.XRealIP, "127.0.0.1")
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
Logger()(h)(c)

// With X-Forwarded-For
req.Header.Del(echo.XRealIP)
req.Header.Add(echo.XForwardedFor, "127.0.0.1")
h = func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
Logger()(h)(c)

// Status 2xx
h = func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
Logger()(h)(c)

// Status 3xx
rec = httptest.NewRecorder()
c = echo.NewContext(req, echo.NewResponse(rec), e)
Expand Down

0 comments on commit 799f01a

Please sign in to comment.