Skip to content

Commit 2e27866

Browse files
authored
Merge pull request #416 from SumoLogic/aj-adding-http-req-logs
SUMO-184022: Adding request logging for HTTP calls to Sumo Logic.
2 parents 5dc283d + a92ff01 commit 2e27866

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

sumologic/sumologic_client.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"io/ioutil"
11+
"log"
1112
"net/http"
1213
"net/url"
1314
"time"
@@ -60,6 +61,12 @@ func createNewRequest(method, url string, body io.Reader, accessID string, acces
6061
return req, nil
6162
}
6263

64+
func logRequestAndResponse(req *http.Request, resp *http.Response) {
65+
var maskedHeader = req.Header.Clone()
66+
maskedHeader.Set("Authorization", "xxxxxxxxxxx")
67+
log.Printf("[DEBUG] Request: [Method=%s] [URL=%s] [Headers=%s]. Response: [StatusCode=%s]\n", req.Method, req.URL, maskedHeader, resp.Status)
68+
}
69+
6370
func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) {
6471
relativeURL, err := url.Parse(urlPath)
6572
if err != nil {
@@ -80,6 +87,8 @@ func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, [
8087

8188
<-rateLimiter.C
8289
resp, err := s.httpClient.Do(req)
90+
logRequestAndResponse(req, resp)
91+
8392
if err != nil {
8493
return nil, nil, err
8594
}
@@ -118,6 +127,8 @@ func (s *Client) GetWithCookies(urlPath string, cookies []*http.Cookie) ([]byte,
118127

119128
<-rateLimiter.C
120129
resp, err := s.httpClient.Do(req)
130+
logRequestAndResponse(req, resp)
131+
121132
if err != nil {
122133
return nil, "", err
123134
}
@@ -153,6 +164,8 @@ func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
153164

154165
<-rateLimiter.C
155166
resp, err := s.httpClient.Do(req)
167+
logRequestAndResponse(req, resp)
168+
156169
if err != nil {
157170
return nil, err
158171
}
@@ -184,6 +197,7 @@ func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error)
184197

185198
<-rateLimiter.C
186199
resp, err := s.httpClient.Do(req)
200+
logRequestAndResponse(req, resp)
187201

188202
if err != nil {
189203
return nil, err
@@ -217,6 +231,8 @@ func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
217231

218232
<-rateLimiter.C
219233
resp, err := s.httpClient.Do(req)
234+
logRequestAndResponse(req, resp)
235+
220236
if err != nil {
221237
return nil, err
222238
}
@@ -253,6 +269,8 @@ func (s *Client) GetWithErrOpt(urlPath string, return404Err bool) ([]byte, strin
253269

254270
<-rateLimiter.C
255271
resp, err := s.httpClient.Do(req)
272+
logRequestAndResponse(req, resp)
273+
256274
if err != nil {
257275
return nil, "", err
258276
}
@@ -291,6 +309,8 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
291309

292310
<-rateLimiter.C
293311
resp, err := s.httpClient.Do(req)
312+
logRequestAndResponse(req, resp)
313+
294314
if err != nil {
295315
return nil, err
296316
}

0 commit comments

Comments
 (0)