Skip to content

Commit 5820ba0

Browse files
committed
SUMO-184022: Logging request and response in client.
1 parent cd432bc commit 5820ba0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sumologic/sumologic_client.go

Lines changed: 21 additions & 1 deletion
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,13 @@ 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 made to Sumo Logic: [Method=%s] [URL=%s] [Headers=%s]", req.Method, req.URL, maskedHeader)
68+
log.Printf("[DEBUG] Response received from Sumo Logic: [StatusCode=%s]", resp.Status)
69+
}
70+
6371
func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, []*http.Cookie, error) {
6472
relativeURL, err := url.Parse(urlPath)
6573
if err != nil {
@@ -80,7 +88,8 @@ func (s *Client) PostWithCookies(urlPath string, payload interface{}) ([]byte, [
8088

8189
<-rateLimiter.C
8290
resp, err := s.httpClient.Do(req)
83-
log.Printf("[DEBUG] Made a request to Sumo Logic: %s", req)
91+
logRequestAndResponse(req, resp)
92+
8493
if err != nil {
8594
return nil, nil, err
8695
}
@@ -119,6 +128,8 @@ func (s *Client) GetWithCookies(urlPath string, cookies []*http.Cookie) ([]byte,
119128

120129
<-rateLimiter.C
121130
resp, err := s.httpClient.Do(req)
131+
logRequestAndResponse(req, resp)
132+
122133
if err != nil {
123134
return nil, "", err
124135
}
@@ -154,6 +165,8 @@ func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
154165

155166
<-rateLimiter.C
156167
resp, err := s.httpClient.Do(req)
168+
logRequestAndResponse(req, resp)
169+
157170
if err != nil {
158171
return nil, err
159172
}
@@ -185,6 +198,7 @@ func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error)
185198

186199
<-rateLimiter.C
187200
resp, err := s.httpClient.Do(req)
201+
logRequestAndResponse(req, resp)
188202

189203
if err != nil {
190204
return nil, err
@@ -218,6 +232,8 @@ func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
218232

219233
<-rateLimiter.C
220234
resp, err := s.httpClient.Do(req)
235+
logRequestAndResponse(req, resp)
236+
221237
if err != nil {
222238
return nil, err
223239
}
@@ -254,6 +270,8 @@ func (s *Client) GetWithErrOpt(urlPath string, return404Err bool) ([]byte, strin
254270

255271
<-rateLimiter.C
256272
resp, err := s.httpClient.Do(req)
273+
logRequestAndResponse(req, resp)
274+
257275
if err != nil {
258276
return nil, "", err
259277
}
@@ -292,6 +310,8 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
292310

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

0 commit comments

Comments
 (0)