How to redirect REST Assured logs to log4j logger?

5,944 views
Skip to first unread message

akshay....@agrostar.in

unread,
Apr 17, 2017, 4:37:14 AM4/17/17
to REST assured
Hello!

I'm using REST Assured for getting response in below manner:

Response response = RestAssured.given().log().method().contentType(ContentType.JSON)...

Also, there's a Logger instance in the same class as:

private Logger log = Logger.getLogger(MyClass.class);

I would like to know if there's some way to redirect the output of log().method(). to my Logger object.

Johan Haleby

unread,
Apr 18, 2017, 1:12:48 AM4/18/17
to rest-a...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

akshay....@agrostar.in

unread,
Apr 18, 2017, 1:44:01 AM4/18/17
to REST assured
Thanks Johan for the response! I tried the solution for printing the method name in my logs (handled by log4j), but it seems to be introducing two blank lines after the method name in logs. I tried to play around a lot with the code in http://stackoverflow.com/a/33388510/398441, but no luck so far. After tweaking the log4j.properties file to print the class name and line number from which the blank lines are getting introduced, I found that they come from below line in http://stackoverflow.com/a/33388510/398441.

myLog.debug( this.myStringBuilder.toString() );



On Tuesday, April 18, 2017 at 10:42:48 AM UTC+5:30, Johan Haleby wrote:
On Mon, Apr 17, 2017 at 10:37 AM, <akshay....@agrostar.in> wrote:
Hello!

I'm using REST Assured for getting response in below manner:

Response response = RestAssured.given().log().method().contentType(ContentType.JSON)...

Also, there's a Logger instance in the same class as:

private Logger log = Logger.getLogger(MyClass.class);

I would like to know if there's some way to redirect the output of log().method(). to my Logger object.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Matt Adamson

unread,
May 2, 2017, 4:28:13 PM5/2/17
to REST assured
Also in terms of 

will redirect all log outputs of a ValidatableResponse to the supplied logger:
 * <pre>
 *             resp.then().log().all( true );
 * </pre>

I wonder if there's a way to make all REST assured requests to log in this way without having to add log().all(true) to each REST assured request?
Message has been deleted

akshay....@agrostar.in

unread,
May 3, 2017, 12:00:41 AM5/3/17
to REST assured
Hi Matt,

Yes, there is a way to do that. You could define a "RequestSpecification" object and re-use the same with every request in this way:

1. Define a "spec" with required details, like logging (with level), etc.
RequestionSpecification spec = new RequestSpecBuilder().log(LogDetail.ALL).build();

2. Use the "spec" in your request to make a GET call (assuming baseURI and basePath have been set somewhere), e.g.
Response response = given().spec(spec).get();

BTW, using "spec" you can do a lot of other things like setting contentType, baseURI, basePath, etc.

Matt Adamson

unread,
May 3, 2017, 5:09:18 AM5/3/17
to REST assured
Thanks yes that's a nice technique reusing a base spec object

tejasvi hegde

unread,
Nov 26, 2017, 10:55:49 PM11/26/17
to REST assured
I was able to successfully extract the request & response specification (logs) using the concept of filters in Rest-Assured. I have created a video on youtube on how to do this. Below is the link. Hope this helps


Regards
Tej

Johan Haleby

unread,
Nov 26, 2017, 11:44:04 PM11/26/17
to rest-a...@googlegroups.com
Really nice, thanks!

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

akshay....@agrostar.in

unread,
Nov 27, 2017, 4:05:08 AM11/27/17
to REST assured
Hi Tej,

This is a really nice example for RA filters. My question was about redirecting the RA logs to log4j though (which I've enabled by implementing a custom LoggerPrintStream).

Thanks!

Andrei Stoica

unread,
Aug 13, 2020, 3:11:59 PM8/13/20
to REST assured
You can use filters. Check an example in this article.
I uses filters and passes the output to log4j2
Reply all
Reply to author
Forward
0 new messages