gin/logger.go

36 lines
509 B
Go
Raw Normal View History

2014-06-18 03:42:34 +04:00
package gin
import (
"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()
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))
if len(c.Errors) > 0 {
2014-07-02 04:31:11 +04:00
fmt.Println(c.Errors.String())
}
2014-06-18 03:42:34 +04:00
}
}