Skip to content

Commit

Permalink
simulators/ethereum/engine: Trim log output of requests (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Oct 24, 2023
1 parent e080138 commit 384794e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions simulators/ethereum/engine/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ type LoggingRoundTrip struct {
Inner http.RoundTripper
}

const MAX_LOG_BYTES = 1024 * 4

func (rt *LoggingRoundTrip) RoundTrip(req *http.Request) (*http.Response, error) {
// Read and log the request body.
reqBytes, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
return nil, err
}
rt.Logger.Logf(">> (%s) %s", rt.ID, bytes.TrimSpace(reqBytes))
reqLogBytes := bytes.TrimSpace(reqBytes[:])
if len(reqLogBytes) > MAX_LOG_BYTES {
rt.Logger.Logf(">> (%s) %s... (Log trimmed)", rt.ID, reqLogBytes[:MAX_LOG_BYTES])
} else {
rt.Logger.Logf(">> (%s) %s", rt.ID, reqLogBytes)
}
reqCopy := *req
reqCopy.Body = io.NopCloser(bytes.NewReader(reqBytes))

Expand All @@ -68,7 +75,12 @@ func (rt *LoggingRoundTrip) RoundTrip(req *http.Request) (*http.Response, error)
}
respCopy := *resp
respCopy.Body = io.NopCloser(bytes.NewReader(respBytes))
rt.Logger.Logf("<< (%s) %s", rt.ID, bytes.TrimSpace(respBytes))
respLogBytes := bytes.TrimSpace(respBytes[:])
if len(respLogBytes) > MAX_LOG_BYTES {
rt.Logger.Logf("<< (%s) %s... (Log trimmed)", rt.ID, respLogBytes[:MAX_LOG_BYTES])
} else {
rt.Logger.Logf("<< (%s) %s", rt.ID, respLogBytes)
}
return &respCopy, nil
}

Expand Down

0 comments on commit 384794e

Please sign in to comment.