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