debug.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2019 Yunion
  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 azure
  15. import (
  16. "net/http"
  17. "net/http/httputil"
  18. "github.com/Azure/go-autorest/autorest"
  19. "yunion.io/x/log"
  20. )
  21. func LogRequest() autorest.PrepareDecorator {
  22. return func(p autorest.Preparer) autorest.Preparer {
  23. return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
  24. r, err := p.Prepare(r)
  25. if err != nil {
  26. log.Errorln(err)
  27. }
  28. auth := r.Header.Get("Authorization")
  29. if len(auth) > 0 {
  30. log.Debugf("Authorization: %s", auth)
  31. }
  32. return r, err
  33. })
  34. }
  35. }
  36. func LogResponse() autorest.RespondDecorator {
  37. return func(p autorest.Responder) autorest.Responder {
  38. return autorest.ResponderFunc(func(r *http.Response) error {
  39. err := p.Respond(r)
  40. if err != nil {
  41. log.Errorln(err)
  42. }
  43. dump, _ := httputil.DumpResponse(r, true)
  44. log.Errorf("%s", string(dump))
  45. return err
  46. })
  47. }
  48. }