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