top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

The Complete Guide to Rest Assured logging with Log method

Let’s begin by understanding what is Rest Assured, It is a JAVA-based library used for Testing REST API’s. In API automation, Rest Assured can be integrated with popular testing frameworks such as Cucumber, TestNG, and JUnit. Rest Assured is an open-source library that supports JSON and XML-based data in web services for testing REST APIs. Rest Assured facilitates various HTTP methods like GET, POST, PUT, DELETE, and PATCH. You can pass parameters (query, path, or form), headers, and cookies along with the request, and then validate the corresponding response, including aspects such as the status code, status line, response time, content type, response body, and more.


Logging in API Testing:

Logging is an essential component in testing that helps to understand the behavior of the API. These components not only provide visibility into the test execution but also ensure the quality is met with smooth continuous integration over time. Effective logging provides deeper insights into HTTP requests and responses, which helps to diagnose issues, monitor API interactions, and ensure smooth communication between client and server.


There are two ways to log data in Rest Assured,

  • RestAssured log()

  • RestAssured Filters


In this blog, we will look deep into the log method in Rest Assured.


Need for Logging data:

Logging isn’t just helpful in writing and testing code, it's equally important to keep the automation stable. Logging enhances the transparency of the test execution by providing detailed insights into each step of the HTTP request and response. It helps in Analyzing, Debugging, and Monitoring.


Analyzing:

Regularly analyze logs to identify patterns or issues that could be causing problems in test automation. Imagine a restaurant owner noticing a drop in customers during March. To understand the cause, analyze customer feedback and weather data. The analysis reveals that poor weather causes reduced tourism. Similarly, analyzing test execution patterns helps identify what went wrong, when it happened, and why, ensuring continuous improvement and leading to more reliable API.


Debugging:

Automated tests can experience unexpected errors or failures for various reasons, such as application change, unstable test environment, or incorrect test data. Debugging ensures active investigation and fixing of current issues encountered during test execution. Imagine the car is not started, you begin to troubleshoot the root cause by checking on the battery, fuel level, ignition system, and other components. On analysis, you discover the battery is dead and replace the battery to start the car. Similarly, we examine the request and response data, to find the cause and resolve it.


Monitoring:

When executing an API test, it is essential to monitor the progress to understand how many tests are passed, failed, or pending. Monitoring continuously helps to detect problems early. Just like a security camera watches over a space and alerts you of unusual activity, real time monitoring keeps an eye on test execution.


ree


Types of Logging in Rest Assured with .log()

Rest Assured offers a built-in method to log different parts of HTTP requests and responses. This can be simply done by printing everything on the console. These are the three primary types of logging supported by the log method,


  • Request logging

  • Response logging

  • Conditional logging


Request Logging:

Request logging displays detailed information about HTTP requests sent to the server during API calls. It ensures that the request is properly configured, which helps in debugging, and analyzing the API. Rest Assured offers several options to log specific part of requests such as URI, headers, body, cookies, method, and parameters.

    public void searchBook() {
        RestAssured.given().log().all() // logs all Request data
                .baseUri(baseURL)
                .header("Content-Type", "application/json")
                .when().get("/GetBook")
                .then().assertThat().statusCode(200);
    }

Let's take a look at the available request log methods,

Method

Request Log

.log().all()

Logs entire HTTP Request data such as base URI, endpoint, headers, cookies, request body, query parameter & request method.

.log().body()

Logs the request content of the body sent to the server, it accepts XML or JSON.

.log().headers()

Logs the headers before the request is being sent.

.log().uri()

Logs the full Uniform Resource Identifier (URI) before the request is being sent.

.log().path()

Logs only the path parameters before the request is sent.

.log().params()

Logs all the parameters such as query, path and form parameters before the request is sent.

.log().method()

Logs the HTTP request method before the request is sent.

.log().cookies()

Logs all the cookies information before the request is sent.

.log().everything()

Logs all HTTP Request data such as base URI, endpoint, headers, cookies, request body, query parameter, and request method. This method is alias of .log().all()


Response logging

Response logging displays detailed information about the HTTP response received from the server. Similar to request logging Rest Assured offers several options to logs specific part of response such as status code, header, body and cookies.

    public void searchBook() {
        RestAssured.given().baseUri(baseURL)
                .header("Content-Type", "application/json")
                .when().get("/GetBook")
                .then().log().body() // log the Response body
                .assertThat().statusCode(200);
    }

Let's take a look at the available response log methods rest assured,

Method

Response Log

.log().all()

Logs entire HTTP Response data received from server such as status line, headers, cookies, and response body.

.log().status()

Logs the status line, which contains the HTTP version, status code, and reason.

.log().headers()

Logs the response headers after the response is received from the server.

.log().body()

Logs the response body received from the server.

.log().cookies()

Logs all the response cookie information that is received from the server.

.log().everything()

Logs all HTTP Response data received from the server such as status line, headers, cookies, and response body. This method is alias of .log().all()


Conditional logging:

Conditional logging displays detailed information about HTTP requests or responses only when specified condition are satisfied. This is very helpful when we want to log information only when unexpected behavior or error occur, such as failed validation or unexpected status code. Conditional logging helps in debugging, analyzing the API by maintaining clean log and focus only on the necessary data.

    public void searchBook() {
        RestAssured.given().baseUri(baseURL)
        		.log().ifValidationFails() // Logs request only if validation fails
                .when().get("/GetBook")
                .then()
			.log().ifError() // Logs the response only if an error occurs
                .assertThat().statusCode(200);
    }

Conditional logging is effective in avoiding extensive log clusters, log-only errors, or unexpected responses in CI/CD environments. Let's take a look at the available conditional log methods in Rest Assured.


Method

Conditional Log

.log().ifError()

Logs the entire response only if the status code falls between 400 and 599 (Client or Server Error). The response data includes the status line, headers, cookies, and response body.

.log().ifValidationsFails()

Logs the entire response if an assertion or validation fails. The response data includes the status line, headers, cookies, and response body.

.log().ifStatusCodeIsEqualTo(xxx)

Logs the entire response only if the status code matches the one provided. The response data includes the status line, headers, cookies and response body.

.log().ifStatusCodeMatches(x.*)

Logs the entire response only if the status code matches the supplied Hamcrest matcher. The response data includes the status line, headers, cookies, and response body.


Key components for developing a logging strategy:

  1. Identify what to log:

    Determine what information is important to log, which part of request or response needs to be logged such as headers, cookies, status line, body.


  2. Decide when to log:

    Determine when to log the information, such as before sending data (restassured given()) to server, after receiving information from the server (restassured then()) or unexpected behavior or error occur in API .


  3. Review and analyze logs:

    Regularly review and analyze logs to identify issues and improve the logging strategy.


I hope in this blog you all learned about the different log methods in Rest Assured and how they can be effectively used to Analyze, Debug, and Monitor the API Request and Response.

‘If learning is an act of exploration, then technology equips the explorer for the journey of a lifetime

Keep learning!!!


+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page