Skip to content

Instantly share code, notes, and snippets.

@Cibernomadas
Created July 3, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cibernomadas/a5084a48fc1478f196d525a88a53e0cb to your computer and use it in GitHub Desktop.
Save Cibernomadas/a5084a48fc1478f196d525a88a53e0cb to your computer and use it in GitHub Desktop.
func RegisterLogger(out *os.File) gin.HandlerFunc {
return func(c *gin.Context) {
// Start timer
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
// 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.String()
if raw != "" {
path = path + "?" + raw
}
fmt.Fprintf(out, "[GIN] %v | %3d | %13v | %15s | %-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