logger.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2013 Martini Authors
  2. // Copyright 2014 Unknwon
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package middleware
  16. import (
  17. "fmt"
  18. "net/http"
  19. "time"
  20. "github.com/Unknwon/macaron"
  21. "github.com/grafana/grafana/pkg/log"
  22. )
  23. func Logger() macaron.Handler {
  24. return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
  25. start := time.Now()
  26. rw := res.(macaron.ResponseWriter)
  27. c.Next()
  28. content := fmt.Sprintf("Completed %s %v %s in %v", req.URL.Path, rw.Status(), http.StatusText(rw.Status()), time.Since(start))
  29. switch rw.Status() {
  30. case 200, 304:
  31. content = fmt.Sprintf("%s", content)
  32. return
  33. case 404:
  34. content = fmt.Sprintf("%s", content)
  35. case 500:
  36. content = fmt.Sprintf("%s", content)
  37. }
  38. log.Info(content)
  39. }
  40. }