yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/debug.go (about)

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