logger.go 509 B

1234567891011121314151617181920212223242526272829303132333435
  1. package gin
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. )
  7. func ErrorLogger() HandlerFunc {
  8. return func(c *Context) {
  9. c.Next()
  10. if len(c.Errors) > 0 {
  11. // -1 status code = do not change current one
  12. c.JSON(-1, c.Errors)
  13. }
  14. }
  15. }
  16. func Logger() HandlerFunc {
  17. return func(c *Context) {
  18. // Start timer
  19. t := time.Now()
  20. // Process request
  21. c.Next()
  22. // Calculate resolution time
  23. log.Printf("%s in %v", c.Req.RequestURI, time.Since(t))
  24. if len(c.Errors) > 0 {
  25. fmt.Println(c.Errors.String())
  26. }
  27. }
  28. }