From c1775e85cc39bbc32f8433e7971fca01bdbe031e Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sun, 6 Jul 2014 18:26:40 +0200 Subject: [PATCH] Adds source IP in built-in logger --- logger.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/logger.go b/logger.go index a593b5f8..7bd87b70 100644 --- a/logger.go +++ b/logger.go @@ -1,7 +1,6 @@ package gin import ( - "fmt" "log" "os" "time" @@ -27,7 +26,9 @@ var ( ) func Logger() HandlerFunc { - logger := log.New(os.Stdout, "", 0) + stdlogger := log.New(os.Stdout, "", 0) + //errlogger := log.New(os.Stderr, "", 0) + return func(c *Context) { // Start timer start := time.Now() @@ -35,6 +36,18 @@ func Logger() HandlerFunc { // Process request c.Next() + // save the IP of the requester + requester := c.Req.Header.Get("X-Real-IP") + // if the requester-header is empty, check the forwarded-header + if requester == "" { + requester = c.Req.Header.Get("X-Forwarded-For") + } + + // if the requester is still empty, use the hard-coded address from the socket + if requester == "" { + requester = c.Req.RemoteAddr + } + var color string code := c.Writer.Status() switch { @@ -48,17 +61,18 @@ func Logger() HandlerFunc { color = red } latency := time.Since(start) - logger.Printf("[GIN] %v |%s %3d %s| %12v | %3.1f%% | %3s | %s\n", + stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %3.1f%% | %s %4s %s\n", time.Now().Format("2006/01/02 - 15:04:05"), color, c.Writer.Status(), reset, latency, c.Engine.CacheStress()*100, + requester, c.Req.Method, c.Req.URL.Path, ) // Calculate resolution time if len(c.Errors) > 0 { - fmt.Println(c.Errors.String()) + stdlogger.Println(c.Errors.String()) } } }