Adds source IP in built-in logger
This commit is contained in:
		
							
								
								
									
										22
									
								
								logger.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								logger.go
									
									
									
									
									
								
							@ -1,7 +1,6 @@
 | 
				
			|||||||
package gin
 | 
					package gin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@ -27,7 +26,9 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Logger() HandlerFunc {
 | 
					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) {
 | 
						return func(c *Context) {
 | 
				
			||||||
		// Start timer
 | 
							// Start timer
 | 
				
			||||||
		start := time.Now()
 | 
							start := time.Now()
 | 
				
			||||||
@ -35,6 +36,18 @@ func Logger() HandlerFunc {
 | 
				
			|||||||
		// Process request
 | 
							// Process request
 | 
				
			||||||
		c.Next()
 | 
							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
 | 
							var color string
 | 
				
			||||||
		code := c.Writer.Status()
 | 
							code := c.Writer.Status()
 | 
				
			||||||
		switch {
 | 
							switch {
 | 
				
			||||||
@ -48,17 +61,18 @@ func Logger() HandlerFunc {
 | 
				
			|||||||
			color = red
 | 
								color = red
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		latency := time.Since(start)
 | 
							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"),
 | 
								time.Now().Format("2006/01/02 - 15:04:05"),
 | 
				
			||||||
			color, c.Writer.Status(), reset,
 | 
								color, c.Writer.Status(), reset,
 | 
				
			||||||
			latency,
 | 
								latency,
 | 
				
			||||||
			c.Engine.CacheStress()*100,
 | 
								c.Engine.CacheStress()*100,
 | 
				
			||||||
 | 
								requester,
 | 
				
			||||||
			c.Req.Method, c.Req.URL.Path,
 | 
								c.Req.Method, c.Req.URL.Path,
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Calculate resolution time
 | 
							// Calculate resolution time
 | 
				
			||||||
		if len(c.Errors) > 0 {
 | 
							if len(c.Errors) > 0 {
 | 
				
			||||||
			fmt.Println(c.Errors.String())
 | 
								stdlogger.Println(c.Errors.String())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user