You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "runtime"
  10. "time"
  11. "github.com/go-martini/martini"
  12. )
  13. var isWindows bool
  14. func init() {
  15. isWindows = runtime.GOOS == "windows"
  16. }
  17. func Logger() martini.Handler {
  18. return func(res http.ResponseWriter, req *http.Request, ctx martini.Context, log *log.Logger) {
  19. start := time.Now()
  20. log.Printf("Started %s %s", req.Method, req.URL.Path)
  21. rw := res.(martini.ResponseWriter)
  22. ctx.Next()
  23. content := fmt.Sprintf("Completed %v %s in %v", rw.Status(), http.StatusText(rw.Status()), time.Since(start))
  24. if !isWindows {
  25. switch rw.Status() {
  26. case 200:
  27. content = fmt.Sprintf("\033[1;32m%s\033[0m", content)
  28. case 304:
  29. content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
  30. case 404:
  31. content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
  32. case 500:
  33. content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
  34. }
  35. }
  36. log.Println(content)
  37. }
  38. }