Streamline Your API Debugging: Rest Assured Logging Mechanism with Filters
- Padmini Elangovan
- Apr 30
- 6 min read
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 web services for testing REST APIs.
Logging in API Testing:
In API Testing, having clear visibility of what was sent and what was received is essential. Logging plays a key role in debugging issues, analyzing trends, tracing failures and supporting ongoing maintenance. Logging is an essential component in testing that helps in understanding the behavior of the API. These components not only provide visibility into the test execution but also ensure the quality is met and 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 Filters
RestAssured log()
In this blog, we will look deep into Rest Assured Log Filters.
Rest Assured Log Filter:
Rest Assured Filters works like interceptors, it allows to inspect and manipulate request and response during API execution. Filters offer a clean and consistent way to extend functionality across multiple requests which results in eliminating code duplication. Filters provide an efficient way to implement reusable logic such as logging, authentication, reporting, or error handling.
Rest Assured log filters offer centralized logging control, detailed visibility, and seamless integration with reporting tools and CI/CD pipeline. It automatically captures complete information on request, and response data and redirects the logs to external files.
Having Log filters in Rest Assured is like taking a car to mechanic, where they run complete analysis. This diagnostic captures all information such as what had happened, when and why. If something fails, we a have detailed record to investigate.
There are three common types of Rest assured build log filters available,
Request Logging Filter
Response Logging Filter
Error Logging Filter
Request Logging Filter:
The request log filter captures 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 behavior. The log contains entire HTTP Request data such as base URI, endpoint, headers, cookies, request body, query parameter, and request method.
Imports required for Request Log Filter:
import io.restassured.filter.log.RequestLoggingFilter;
public RequestSpecification requestSpecWithAuth()
{
reqSpec = new RequestSpecBuilder()
.addHeader("Authorization","Bearer "+context.get("token"))
.setBaseUri(baseURL)
.addFilter(RequestLoggingFilter.logRequestTo(log))
.setContentType(ContentType.JSON).build();
return reqSpec;
}Response Logging Filter:
The response log filter captures detailed information about the HTTP response received from the server. Similar to the Rest Assured request log filter, it logs entire response data such as status line, headers, cookies, and response body that is important for debugging and verifying API behavior.
Imports required for Response Log Filter:
import io.restassured.filter.log.ResponseLoggingFilter;
public RequestSpecification requestSpecWithAuth()
{
reqSpec = new RequestSpecBuilder()
.addHeader("Authorization","Bearer "+context.get("token"))
.setBaseUri(baseURL)
.addFilter(ResponseLoggingFilter.logResponseTo(log))
.setContentType(ContentType.JSON).build();
return reqSpec;
}Error Logging Filter:
Error log filter captures detailed information about HTTP requests and responses only on failure. This is very helpful when we want to log information only when unexpected behavior or error occurs, such as failed validation or unexpected status code. Error logging filter helps in debugging, and analyzing the API by maintaining a clean log and focusing only on the necessary data. Error logging filter ensures the entire request and response details are logged only if status code falls between 400 to 599 (Client or Server Error).
Imports required for Error Log Filter:
import io.restassured.filter.log.ErrorLoggingFilter;
public RequestSpecification requestSpecWithAuth()
{
reqSpec = new RequestSpecBuilder()
.addHeader("Authorization","Bearer "+context.get("token"))
.setBaseUri(baseURL)
.addFilter(ErrorLoggingFilter.logErrorsTo(log))
.setContentType(ContentType.JSON).build();
return reqSpec;
}Rest Assured Log File:
PrintStream helps to log data, such as request and response information to an external file in a structured and readable format. New FileOutputStream, opens a file to write data or automatically creates a file if it doesn’t already exist in the specified path. The second parameter true enables append mode. It ensures new data is added to the end of the file by not overwritten the existing content.

String FilePath = "RESTApiLog/RestAssuredLog.txt";
PrintStream log;
public ClassName() throws FileNotFoundException
{
log = new PrintStream(new FileOutputStream(FilePath, true));
}The following is the screenshot of log the file that is generated.

Append Log File to Reporting Tool:
Rest Assured supports appending log to external sources like Allure, Extent, and Cucumber reports. To allure attachment method helps to attach the external log files to the report. Below is the code for appending the log to Allure,
// Attach Log File to Allure report
public void AllureAppendLogFile() throws IOException {
File logFile = new File(FilePath);
if (logFile.exists() && logFile.length() > 0) {
Allure.addAttachment("APILog", new FileInputStream(logFile));
}
}Benefits of using Log Filters over the basic log method:
The basic .log() method is convenient for quick debugging, but the real world automation framework requires a structured logging strategy. Rest Assured Log filters provide this structure, ensuring scalability and maintainability across the entire test execution. Now let’s look into the key benefits of using Rest Assured log filters,
1. Reusable Configuration:
Instead of adding logging to each test, you can centralize logging with the reusable method. We can apply the same logging behavior across all the tests effortlessly with Specification Builder. Having log filters at the specification level helps to manage, review, and debug in a single place.
2. Output to External Logs (Files):
Rest Assured Filters send logs to external files. Filters allow to capture detailed request and response information to log files ensuring easier storage, reporting, and sharing. Filters allow debugging failed tests without scrolling through the console. Rest Assured log filters use Print Stream to save data to a file. Logging to files enhance CI/CD pipelines, Log retention, review, and integration with the reporting tool.
3. Enhanced Error Handling:
Rest Assured Error Logging Filter logs requests and responses only when an error occurs, making logs clean and maintainable. It is more useful in CI/CD pipelines. It improves error handling by providing detailed HTTP traces when the pipeline fails, making it easy to identify issues.
4. Better CI/CD Integration:
By having logs to output files, we can attach logs to external resources like the Allure report, Extent report, or even directly to Jenkins build. This helps maintain a detailed execution history within the CI/CD environment.
Best Practice for using Filters in Rest Assured:
Full Log Coverage:
Combining RequestLoggingFilter and ResponseLoggingFilter ensures full coverage of HTTP request and HTTP response. This allows to capture complete API interaction in a single log file.
Centralize Filters - Spec Builder:
Having filters defined in RequestSpecBuilder helps remove repetitive filter declarations in every test. Reusable Spec configuration results in cleaner and more efficient code.
Custom Log Filter:
Custom filter in rest assured allows to modify or log the request or response. Filters allow us to implement specific logic for testing such as data masking, advanced logging, performance tracking, and dynamic header management.
Selective Error Logging:
Instead of logging entire request and response data for all API interactions, use ErrorLoggingFilter to log data only if the response code falls between 400 and 599(client or server errors). It is useful in CI/CD to maintain clean logs and focuses only on failed tests for faster debugging.
Unique Log File:
Using meaningful file names helps maintain an organized log, especially when running tests in parallel or within CI/CD pipeline. Dynamically name log file by timestamp, scenario name, or unique identifier ensuring easy tracking and identification of each test run.
Attach Log to Report:
Attach external log file to Allure, cucumber, or extent reports for easy access and analysis of API interactions. It helps in debugging failure and reviewing test results within the reporting tool.
Track Time and Performance:
We can customize filters to track response times and log performance trends. By integrating with CI/CD pipeline, we can gather logs over multiple test executions to monitor response time trends. Reporting tools like Allure or Extent helps in visualizing performance trends over time.
I hope this blog you all learned about the importance of Log filters in Rest Assured and how they can be effectively used to Analyze, Debug, Monitor, and Maintain 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!!!


