Skip to content

Commit

Permalink
fix debug log
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Oct 11, 2024
1 parent b353e40 commit 2dcf66c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func WithLoggerLevel(ctx context.Context, level logrus.Level) context.Context {

// create logger
logger := logrus.New()
formatter.DisableQuote = true
logger.SetFormatter(&formatter)
logger.SetLevel(level)

Expand Down
25 changes: 13 additions & 12 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,23 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
e := log.GetLogger(ctx)

// logs to be printed out
logs := fmt.Sprintf("> Request: %q %q\n", req.Method, req.URL)
logs = logs + "> Request headers:\n"
logs = logs + logHeader(req.Header)
var logs strings.Builder
fmt.Fprintf(&logs, "> Request: %q %q\n", req.Method, req.URL)
fmt.Fprintln(&logs, "> Request headers:")
fmt.Fprintln(&logs, logHeader(req.Header))

resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
e.Debugf(logs)
e.Debug(logs.String())
e.Errorf("Error in getting response: %w", err)
} else if resp == nil {
e.Debugf(logs)
e.Debug(logs.String())
e.Errorf("No response obtained for request %s %q", req.Method, req.URL)
} else {
logs = logs + fmt.Sprintf("< Response status: %q\n", resp.Status)
logs = logs + "< Response headers:\n"
logs = logs + logHeader(resp.Header)
e.Debugf(logs)
fmt.Fprintf(&logs, "< Response status: %q\n", resp.Status)
fmt.Fprintln(&logs, "< Response headers:")
fmt.Fprintln(&logs, logHeader(resp.Header))
e.Debug(logs.String())
}
return resp, err
}
Expand All @@ -79,14 +80,14 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
// scrubbed.
func logHeader(header http.Header) string {
if len(header) > 0 {
var logs string
var logs strings.Builder
for k, v := range header {
if strings.EqualFold(k, "Authorization") {
v = []string{"*****"}
}
logs = logs + fmt.Sprintf(" %q: %q\n", k, strings.Join(v, ", "))
fmt.Fprintf(&logs, " %q: %q\n", k, strings.Join(v, ", "))
}
return logs
return logs.String()
} else {
return " Empty header"
}
Expand Down

0 comments on commit 2dcf66c

Please sign in to comment.