Skip to content

Commit

Permalink
aws/client: Fix logging to allow it to be enabled per operation (#3778)
Browse files Browse the repository at this point in the history
Allow logging of operation and request to be enabled per operation, not
only per client or session.

	client := dynamodb.New(sess)

	var w bytes.Buffer
	resp, err := client.DescribeTableWithContext(context.TODO(), params,
		func(r *request.Request) {
			r.Config.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
			r.Config.Logger = aws.LoggerFunc(func(args ...interface{}) {
				fmt.Fprintln(&w, args...)
			})
		})
	if someCondition {
		log.Printf("Condition met\n%s", w.String())
	}
  • Loading branch information
jasdel authored Feb 16, 2021
1 parent 8756523 commit 606788c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
### SDK Enhancements

### SDK Bugs
* `aws/client`: Fix logging to allow it to be enabled per operation
* Allow logging of operation and request to be enabled per operation, not only per client or session.
4 changes: 0 additions & 4 deletions aws/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ func (c *Client) NewRequest(operation *request.Operation, params interface{}, da
// AddDebugHandlers injects debug logging handlers into the service to log request
// debug information.
func (c *Client) AddDebugHandlers() {
if !c.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

c.Handlers.Send.PushFrontNamed(LogHTTPRequestHandler)
c.Handlers.Send.PushBackNamed(LogHTTPResponseHandler)
}
8 changes: 5 additions & 3 deletions aws/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ func TestNewClient_CopyHandlers(t *testing.T) {
if e, a := 2, handlers.Send.Len(); e != a {
t.Errorf("expect %d original handlers, got %d", e, a)
}
if e, a := 3, c.Handlers.Send.Len(); e != a {
if e, a := 5, c.Handlers.Send.Len(); e != a {
t.Errorf("expect %d client handlers, got %d", e, a)
}

handlers.Send.Run(nil)
req := c.NewRequest(&request.Operation{}, struct{}{}, struct{}{})

handlers.Send.Run(req)
if !*firstCalled {
t.Errorf("expect first handler to of been called")
}
Expand All @@ -64,7 +66,7 @@ func TestNewClient_CopyHandlers(t *testing.T) {
t.Errorf("expect client handler to not of been called, but was")
}

c.Handlers.Send.Run(nil)
c.Handlers.Send.Run(req)
if !*firstCalled {
t.Errorf("expect client's first handler to of been called")
}
Expand Down
8 changes: 8 additions & 0 deletions aws/client/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var LogHTTPRequestHandler = request.NamedHandler{
}

func logRequest(r *request.Request) {
if !r.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

logBody := r.Config.LogLevel.Matches(aws.LogDebugWithHTTPBody)
bodySeekable := aws.IsReaderSeekable(r.Body)

Expand Down Expand Up @@ -120,6 +124,10 @@ var LogHTTPResponseHandler = request.NamedHandler{
}

func logResponse(r *request.Request) {
if !r.Config.LogLevel.AtLeast(aws.LogDebug) {
return
}

lw := &logWriter{r.Config.Logger, bytes.NewBuffer(nil)}

if r.HTTPResponse == nil {
Expand Down
7 changes: 6 additions & 1 deletion aws/client/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestLogResponse(t *testing.T) {
ExpectBody []byte
ReadBody bool
LogLevel aws.LogLevelType
ExpectLog bool
}{
{
Body: bytes.NewBuffer([]byte("body content")),
Expand All @@ -142,11 +143,13 @@ func TestLogResponse(t *testing.T) {
{
Body: bytes.NewBuffer([]byte("body content")),
LogLevel: aws.LogDebug,
ExpectLog: true,
ExpectBody: []byte("body content"),
},
{
Body: bytes.NewBuffer([]byte("body content")),
LogLevel: aws.LogDebugWithHTTPBody,
ExpectLog: true,
ReadBody: true,
ExpectBody: []byte("body content"),
},
Expand Down Expand Up @@ -190,8 +193,10 @@ func TestLogResponse(t *testing.T) {
}
}

if logW.Len() == 0 {
if c.ExpectLog && logW.Len() == 0 {
t.Errorf("%d, expect HTTP Response headers to be logged", i)
} else if !c.ExpectLog && logW.Len() != 0 {
t.Errorf("%d, expect no log, got,\n%v", i, logW.String())
}

b, err := ioutil.ReadAll(req.HTTPResponse.Body)
Expand Down

0 comments on commit 606788c

Please sign in to comment.