|
| 1 | +// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. |
| 2 | +// resty source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +package resty |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +type ( |
| 15 | + // DebugLogCallbackFunc function type is for request and response debug log callback purposes. |
| 16 | + // It gets called before Resty logs it |
| 17 | + DebugLogCallbackFunc func(*DebugLog) |
| 18 | + |
| 19 | + // DebugLogFormatterFunc function type is used to implement debug log formatting. |
| 20 | + // See out of the box [DebugLogStringFormatter], [DebugLogJSONFormatter] |
| 21 | + DebugLogFormatterFunc func(*DebugLog) string |
| 22 | + |
| 23 | + // DebugLog struct is used to collect details from Resty request and response |
| 24 | + // for debug logging callback purposes. |
| 25 | + DebugLog struct { |
| 26 | + Request *DebugLogRequest `json:"request"` |
| 27 | + Response *DebugLogResponse `json:"response"` |
| 28 | + TraceInfo *TraceInfo `json:"trace_info"` |
| 29 | + } |
| 30 | + |
| 31 | + // DebugLogRequest type used to capture debug info about the [Request]. |
| 32 | + DebugLogRequest struct { |
| 33 | + Host string `json:"host"` |
| 34 | + URI string `json:"uri"` |
| 35 | + Method string `json:"method"` |
| 36 | + Proto string `json:"proto"` |
| 37 | + Header http.Header `json:"header"` |
| 38 | + CurlCmd string `json:"curl_cmd"` |
| 39 | + RetryTraceID string `json:"retry_trace_id"` |
| 40 | + Attempt int `json:"attempt"` |
| 41 | + Body string `json:"body"` |
| 42 | + } |
| 43 | + |
| 44 | + // DebugLogResponse type used to capture debug info about the [Response]. |
| 45 | + DebugLogResponse struct { |
| 46 | + StatusCode int `json:"status_code"` |
| 47 | + Status string `json:"status"` |
| 48 | + Proto string `json:"proto"` |
| 49 | + ReceivedAt time.Time `json:"received_at"` |
| 50 | + Duration time.Duration `json:"duration"` |
| 51 | + Size int64 `json:"size"` |
| 52 | + Header http.Header `json:"header"` |
| 53 | + Body string `json:"body"` |
| 54 | + } |
| 55 | +) |
| 56 | + |
| 57 | +// DebugLogFormatter function formats the given debug log info in human readable |
| 58 | +// format. |
| 59 | +// |
| 60 | +// This is the default debug log formatter in the Resty. |
| 61 | +func DebugLogFormatter(dl *DebugLog) string { |
| 62 | + debugLog := "\n==============================================================================\n" |
| 63 | + |
| 64 | + req := dl.Request |
| 65 | + if len(req.CurlCmd) > 0 { |
| 66 | + debugLog += "~~~ REQUEST(CURL) ~~~\n" + |
| 67 | + fmt.Sprintf(" %v\n", req.CurlCmd) |
| 68 | + } |
| 69 | + debugLog += "~~~ REQUEST ~~~\n" + |
| 70 | + fmt.Sprintf("%s %s %s\n", req.Method, req.URI, req.Proto) + |
| 71 | + fmt.Sprintf("HOST : %s\n", req.Host) + |
| 72 | + fmt.Sprintf("HEADERS:\n%s\n", composeHeaders(req.Header)) + |
| 73 | + fmt.Sprintf("BODY :\n%v\n", req.Body) + |
| 74 | + "------------------------------------------------------------------------------\n" |
| 75 | + if len(req.RetryTraceID) > 0 { |
| 76 | + debugLog += fmt.Sprintf("RETRY TRACE ID: %s\n", req.RetryTraceID) + |
| 77 | + fmt.Sprintf("ATTEMPT : %d\n", req.Attempt) + |
| 78 | + "------------------------------------------------------------------------------\n" |
| 79 | + } |
| 80 | + |
| 81 | + res := dl.Response |
| 82 | + debugLog += "~~~ RESPONSE ~~~\n" + |
| 83 | + fmt.Sprintf("STATUS : %s\n", res.Status) + |
| 84 | + fmt.Sprintf("PROTO : %s\n", res.Proto) + |
| 85 | + fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt.Format(time.RFC3339Nano)) + |
| 86 | + fmt.Sprintf("DURATION : %v\n", res.Duration) + |
| 87 | + "HEADERS :\n" + |
| 88 | + composeHeaders(res.Header) + "\n" + |
| 89 | + fmt.Sprintf("BODY :\n%v\n", res.Body) |
| 90 | + if dl.TraceInfo != nil { |
| 91 | + debugLog += "------------------------------------------------------------------------------\n" |
| 92 | + debugLog += fmt.Sprintf("%v\n", dl.TraceInfo) |
| 93 | + } |
| 94 | + debugLog += "==============================================================================\n" |
| 95 | + |
| 96 | + return debugLog |
| 97 | +} |
| 98 | + |
| 99 | +// DebugLogJSONFormatter function formats the given debug log info in JSON format. |
| 100 | +func DebugLogJSONFormatter(dl *DebugLog) string { |
| 101 | + return toJSON(dl) |
| 102 | +} |
| 103 | + |
| 104 | +func debugLogger(c *Client, res *Response) { |
| 105 | + req := res.Request |
| 106 | + if !req.Debug { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + rdl := &DebugLogResponse{ |
| 111 | + StatusCode: res.StatusCode(), |
| 112 | + Status: res.Status(), |
| 113 | + Proto: res.Proto(), |
| 114 | + ReceivedAt: res.ReceivedAt(), |
| 115 | + Duration: res.Time(), |
| 116 | + Size: res.Size(), |
| 117 | + Header: sanitizeHeaders(res.Header().Clone()), |
| 118 | + Body: res.fmtBodyString(res.Request.DebugBodyLimit), |
| 119 | + } |
| 120 | + |
| 121 | + dl := &DebugLog{ |
| 122 | + Request: req.values[debugRequestLogKey].(*DebugLogRequest), |
| 123 | + Response: rdl, |
| 124 | + } |
| 125 | + |
| 126 | + if res.Request.IsTrace { |
| 127 | + ti := req.TraceInfo() |
| 128 | + dl.TraceInfo = &ti |
| 129 | + } |
| 130 | + |
| 131 | + dblCallback := c.debugLogCallbackFunc() |
| 132 | + if dblCallback != nil { |
| 133 | + dblCallback(dl) |
| 134 | + } |
| 135 | + |
| 136 | + formatterFunc := c.debugLogFormatterFunc() |
| 137 | + if formatterFunc != nil { |
| 138 | + debugLog := formatterFunc(dl) |
| 139 | + req.log.Debugf("%s", debugLog) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +const debugRequestLogKey = "__restyDebugRequestLog" |
| 144 | + |
| 145 | +func prepareRequestDebugInfo(c *Client, r *Request) { |
| 146 | + if !r.Debug { |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + rr := r.RawRequest |
| 151 | + rh := rr.Header.Clone() |
| 152 | + if c.Client().Jar != nil { |
| 153 | + for _, cookie := range c.Client().Jar.Cookies(r.RawRequest.URL) { |
| 154 | + s := fmt.Sprintf("%s=%s", cookie.Name, cookie.Value) |
| 155 | + if c := rh.Get(hdrCookieKey); isStringEmpty(c) { |
| 156 | + rh.Set(hdrCookieKey, s) |
| 157 | + } else { |
| 158 | + rh.Set(hdrCookieKey, c+"; "+s) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + rdl := &DebugLogRequest{ |
| 164 | + Host: rr.URL.Host, |
| 165 | + URI: rr.URL.RequestURI(), |
| 166 | + Method: r.Method, |
| 167 | + Proto: rr.Proto, |
| 168 | + Header: sanitizeHeaders(rh), |
| 169 | + Body: r.fmtBodyString(r.DebugBodyLimit), |
| 170 | + } |
| 171 | + if r.generateCurlCmd && r.debugLogCurlCmd { |
| 172 | + rdl.CurlCmd = r.resultCurlCmd |
| 173 | + } |
| 174 | + if len(r.RetryTraceID) > 0 { |
| 175 | + rdl.Attempt = r.Attempt |
| 176 | + rdl.RetryTraceID = r.RetryTraceID |
| 177 | + } |
| 178 | + |
| 179 | + r.initValuesMap() |
| 180 | + r.values[debugRequestLogKey] = rdl |
| 181 | +} |
0 commit comments