gin/logger.go

36 lines
509 B
Go
Raw Normal View History

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