← Back to libraryQuestion 242 of 468
🔌REST AssuredBeginner

Logging Requests and Responses

📌 Definition:

REST Assured can log request and response details for debugging with .log() methods: log().all(), log().body(), log().headers(), and conditional logging like log().ifValidationFails().

📖 Detailed Explanation:

On the request side, given().log().all() prints method, URI, headers, params, and body. On the response, then().log().body() or .log().all() prints the response. For cleaner output, .log().ifValidationFails() logs only when an assertion fails — ideal for CI so passing tests stay quiet. You can also enable global logging (RestAssured.enableLoggingOfRequestAndResponseIfValidationFails()). Logging is invaluable when a test fails and you need to see exactly what was sent/received, but logging everything on every test creates noise, so prefer conditional logging in suites.

🔑 Key Points:
  • given().log().all() logs the request; then().log().body() the response
  • log().ifValidationFails() logs only on failure (quiet CI)
  • enableLoggingOfRequestAndResponseIfValidationFails() globally
  • Prefer conditional logging to avoid noisy passing tests
🌍 Real-World Example:

Debugging a failing assertion: adding .log().ifValidationFails() to both given() and then() prints the full request/response only for the failing case, making the mismatch obvious without flooding CI logs.

🎯 Scenario-Based Interview Question:

Your CI logs are flooded because every API test prints full request/response. How do you keep useful debug output without the noise?