Skip to content

Instantly share code, notes, and snippets.

@logrusorgru
Created March 10, 2016 09:12
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 logrusorgru/f44878579013c4f210eb to your computer and use it in GitHub Desktop.
Save logrusorgru/f44878579013c4f210eb to your computer and use it in GitHub Desktop.
Gin logger without colors
/*
// 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