mirror of
https://github.com/labstack/echo.git
synced 2025-05-13 22:06:36 +02:00
Adding IP Addresses in log entries
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
27cedaf411
commit
799f01a87b
3
echo.go
3
echo.go
@ -135,7 +135,8 @@ const (
|
|||||||
Upgrade = "Upgrade"
|
Upgrade = "Upgrade"
|
||||||
Vary = "Vary"
|
Vary = "Vary"
|
||||||
WWWAuthenticate = "WWW-Authenticate"
|
WWWAuthenticate = "WWW-Authenticate"
|
||||||
|
XForwardedFor = "X-Forwarded-For"
|
||||||
|
XRealIP = "X-Real-IP"
|
||||||
//-----------
|
//-----------
|
||||||
// Protocols
|
// Protocols
|
||||||
//-----------
|
//-----------
|
||||||
|
@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
@ -11,19 +12,30 @@ import (
|
|||||||
func Logger() echo.MiddlewareFunc {
|
func Logger() echo.MiddlewareFunc {
|
||||||
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c *echo.Context) error {
|
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()
|
start := time.Now()
|
||||||
if err := h(c); err != nil {
|
if err := h(c); err != nil {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
}
|
}
|
||||||
stop := time.Now()
|
stop := time.Now()
|
||||||
method := c.Request().Method
|
method := req.Method
|
||||||
path := c.Request().URL.Path
|
path := req.URL.Path
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = "/"
|
path = "/"
|
||||||
}
|
}
|
||||||
size := c.Response().Size()
|
size := res.Size()
|
||||||
|
|
||||||
n := c.Response().Status()
|
n := res.Status()
|
||||||
code := color.Green(n)
|
code := color.Green(n)
|
||||||
switch {
|
switch {
|
||||||
case n >= 500:
|
case n >= 500:
|
||||||
@ -34,7 +46,7 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
code = color.Cyan(n)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,24 +2,41 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/labstack/echo"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogger(t *testing.T) {
|
func TestLogger(t *testing.T) {
|
||||||
|
// Note: Just for the test coverage, not a real test.
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
req, _ := http.NewRequest(echo.GET, "/", nil)
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := echo.NewContext(req, echo.NewResponse(rec), e)
|
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 {
|
h := func(c *echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
}
|
}
|
||||||
Logger()(h)(c)
|
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
|
// Status 3xx
|
||||||
rec = httptest.NewRecorder()
|
rec = httptest.NewRecorder()
|
||||||
c = echo.NewContext(req, echo.NewResponse(rec), e)
|
c = echo.NewContext(req, echo.NewResponse(rec), e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user