gin/logger.go

36 lines
500 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 {
fmt.Println(c.Errors)
}
2014-06-18 03:42:34 +04:00
}
}