Browse Source

fix:allow user to print response body to logger

tags/1.60.64
wenzuochao 7 months ago
parent
commit
d90ec8385d
6 changed files with 8 additions and 12 deletions
  1. +1
    -0
      docs/7-Logger-CN.md
  2. +1
    -0
      docs/7-Logger-EN.md
  3. +2
    -0
      sdk/client.go
  4. +3
    -4
      sdk/client_test.go
  5. +1
    -1
      sdk/logger.go
  6. +0
    -7
      sdk/responses/response.go

+ 1
- 0
docs/7-Logger-CN.md View File

@@ -50,6 +50,7 @@ client.GetTemplate() // 获取当前的日志模板,若此时客户
| {pid} | PID |
| {cost} | 耗时 |
| {start_time} | 开始时间 |
| {res_body} | 响应主体 |

***
[← 调试](6-Debug-CN.md) | 日志[(English)](7-Logger-EN.md) | [并发 →](8-Concurrent-CN.md)

+ 1
- 0
docs/7-Logger-EN.md View File

@@ -51,6 +51,7 @@ client.GetTemplate() // Get client logger template,if clien logger i
| {pid} | PID |
| {cost} | Cost Time |
| {start_time} | start Time |
| {res_body} | Response body |

***
[← Debug](6-Debug-EN.md) | Logger[(中文)](7-Logger-CN.md) | [Concurrent →](8-Concurrent-EN.md)

+ 2
- 0
sdk/client.go View File

@@ -552,6 +552,8 @@ func (client *Client) DoActionWithSigner(request requests.AcsRequest, response r
}

err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
fieldMap["{res_body}"] = response.GetHttpContentString()
debug("%s", response.GetHttpContentString())
// wrap server errors
if serverErr, ok := err.(*errors.ServerError); ok {
var wrapInfo = map[string]string{}


+ 3
- 4
sdk/client_test.go View File

@@ -470,16 +470,15 @@ func Test_DoAction_WithLogger(t *testing.T) {

// Test when open logger, it will print log.
client.OpenLogger()
template := "{channel}: \"{method} {code}"
template := "{channel}: \"{method} {code} {res_body}"
client.SetTemplate(template)
err = client.DoAction(request, response)
assert.NotNil(t, err)
log = client.GetLogger()
assert.Equal(t, 500, response.GetHttpStatus())
assert.Equal(t, true, log.isOpen)
assert.Equal(t, "{channel}: \"{method} {code}", client.GetTemplate())
assert.Equal(t, client.GetLoggerMsg(), `Alibaba: "GET 500`)
assert.Equal(t, "Server Internel Error", response.GetHttpContentString())
assert.Equal(t, "{channel}: \"{method} {code} {res_body}", client.GetTemplate())
assert.Equal(t, client.GetLoggerMsg(), `Alibaba: "GET 500 Server Internel Error`)
}

func TestClient_BuildRequestWithSigner(t *testing.T) {


+ 1
- 1
sdk/logger.go View File

@@ -21,7 +21,7 @@ type Logger struct {
}

var defaultLoggerTemplate = `{time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {hostname}`
var loggerParam = []string{"{time}", "{start_time}", "{ts}", "{channel}", "{pid}", "{host}", "{method}", "{uri}", "{version}", "{target}", "{hostname}", "{code}", "{error}", "{req_headers}", "{res_headers}", "{cost}"}
var loggerParam = []string{"{time}", "{start_time}", "{ts}", "{channel}", "{pid}", "{host}", "{method}", "{uri}", "{version}", "{target}", "{hostname}", "{code}", "{error}", "{req_headers}", "{res_body}", "{res_headers}", "{cost}"}

func initLogMsg(fieldMap map[string]string) {
for _, value := range loggerParam {


+ 0
- 7
sdk/responses/response.go View File

@@ -23,7 +23,6 @@ import (
"strings"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
)

type AcsResponse interface {
@@ -36,11 +35,6 @@ type AcsResponse interface {
parseFromHttpResponse(httpResponse *http.Response) error
}

var debug utils.Debug

func init() {
debug = utils.Init("sdk")
}
// Unmarshal object from http response body to target Response
func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) (err error) {
err = response.parseFromHttpResponse(httpResponse)
@@ -115,7 +109,6 @@ func (baseResponse *BaseResponse) parseFromHttpResponse(httpResponse *http.Respo
if err != nil {
return
}
debug("%s", string(body))
baseResponse.httpStatus = httpResponse.StatusCode
baseResponse.httpHeaders = httpResponse.Header
baseResponse.httpContentBytes = body


Loading…
Cancel
Save