2014-06-17 23:42:34 +00:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
2014-06-30 02:04:45 +00:00
|
|
|
"fmt"
|
2014-06-17 23:42:34 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-06-30 01:59:21 +00:00
|
|
|
func ErrorLogger() HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
c.Next()
|
2014-06-30 02:04:45 +00:00
|
|
|
|
|
|
|
if len(c.Errors) > 0 {
|
|
|
|
// -1 status code = do not change current one
|
|
|
|
c.JSON(-1, c.Errors)
|
|
|
|
}
|
2014-06-30 01:59:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:42:34 +00:00
|
|
|
func Logger() HandlerFunc {
|
|
|
|
return func(c *Context) {
|
|
|
|
|
|
|
|
// Start timer
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
// Process request
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
// Calculate resolution time
|
2014-06-30 01:59:00 +00:00
|
|
|
log.Printf("%s in %v", c.Req.RequestURI, time.Since(t))
|
2014-06-30 02:04:45 +00:00
|
|
|
if len(c.Errors) > 0 {
|
|
|
|
fmt.Println(c.Errors)
|
|
|
|
}
|
2014-06-17 23:42:34 +00:00
|
|
|
}
|
|
|
|
}
|