http_logger.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package remote
  15. import (
  16. log "github.com/golang/glog"
  17. "net/http"
  18. )
  19. type httpResp struct {
  20. writer http.ResponseWriter
  21. status int
  22. }
  23. func (r *httpResp) Header() http.Header {
  24. return r.writer.Header()
  25. }
  26. func (r *httpResp) Write(d []byte) (int, error) {
  27. return r.writer.Write(d)
  28. }
  29. func (r *httpResp) WriteHeader(status int) {
  30. r.status = status
  31. r.writer.WriteHeader(status)
  32. }
  33. type httpLoggerHandler struct {
  34. h http.Handler
  35. }
  36. func (lh httpLoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  37. resp := &httpResp{w, 0}
  38. lh.h.ServeHTTP(resp, r)
  39. log.Infof("%v %v - %v", r.Method, r.RequestURI, resp.status)
  40. }
  41. func httpLogger(h http.Handler) http.Handler {
  42. return httpLoggerHandler{h}
  43. }