middleware.go 556 B

12345678910111213141516171819202122
  1. package std
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "time"
  7. "github.com/julienschmidt/httprouter"
  8. )
  9. // Logger is a middleware that prints http logs.
  10. func Logger(h httprouter.Handle) httprouter.Handle {
  11. return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
  12. ip, _, _ := net.SplitHostPort(r.RemoteAddr)
  13. ts := time.Now()
  14. h(w, r, params)
  15. te := time.Since(ts)
  16. fmt.Printf("\x1b[%d;%dm[GW]\x1b[0m %s | %d | %v | %s | %s %s\n", 44, 37,
  17. time.Now().Format("2006-01-02 - 15:04:05"), 200, te, ip, r.Method, r.URL.Path)
  18. }
  19. }