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.

52 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. "github.com/gogits/gogs/modules/base"
  13. )
  14. var isWindows bool
  15. func init() {
  16. isWindows = runtime.GOOS == "windows"
  17. }
  18. func Logger() martini.Handler {
  19. return func(res http.ResponseWriter, req *http.Request, ctx martini.Context, log *log.Logger) {
  20. if base.DisableRouterLog {
  21. return
  22. }
  23. start := time.Now()
  24. log.Printf("Started %s %s", req.Method, req.URL.Path)
  25. rw := res.(martini.ResponseWriter)
  26. ctx.Next()
  27. content := fmt.Sprintf("Completed %v %s in %v", rw.Status(), http.StatusText(rw.Status()), time.Since(start))
  28. if !isWindows {
  29. switch rw.Status() {
  30. case 200:
  31. content = fmt.Sprintf("\033[1;32m%s\033[0m", content)
  32. case 304:
  33. content = fmt.Sprintf("\033[1;33m%s\033[0m", content)
  34. case 404:
  35. content = fmt.Sprintf("\033[1;31m%s\033[0m", content)
  36. case 500:
  37. content = fmt.Sprintf("\033[1;36m%s\033[0m", content)
  38. }
  39. }
  40. log.Println(content)
  41. }
  42. }