Created
March 10, 2016 09:12
-
-
Save logrusorgru/f44878579013c4f210eb to your computer and use it in GitHub Desktop.
Gin logger without colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Creates a router without any middleware by default | |
r := gin.New() | |
r.Use(gin.Recovery()) | |
r.Use(logger.Logger()) // use this logger without colors | |
*/ | |
package logger | |
import ( | |
"github.com/gin-gonic/gin" | |
"fmt" | |
"os" | |
) | |
func Logger() gin.HandlerFunc { | |
return func(c *Context) { | |
// Start timer | |
start := time.Now() | |
path := c.Request.URL.Path | |
// Process request | |
c.Next() | |
// Stop timer | |
end := time.Now() | |
latency := end.Sub(start) | |
clientIP := c.ClientIP() | |
method := c.Request.Method | |
statusCode := c.Writer.Status() | |
comment := c.Errors.ByType(gin.ErrorTypePrivate).String() | |
fmt.Fprintf(os.Stdout, | |
"[GIN] %v | %3d | %13v | %s | %-7s %s\n%s", | |
end.Format("2006/01/02 - 15:04:05"), | |
statusCode, | |
latency, | |
clientIP, | |
method, | |
path, | |
comment, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment