There's plenty of ways to do this, from plain system.out.println() calls, whipping out some scala-code to save it to a file using your favourite java libraries, but depending on what you need the response bodies for, the easiest may be to let the logback.xml configuration do it for you.
If you've built your project from the gatling maven archetype it will already contain a logback.xml with a few commented out lines of code that contain appenders that print the entire http request/response to the console, comment in those. If you just need to see the responsebody in order to develop or debug the simulation, enabling one of the sets of these may be exactly what you need:
<!--Uncomment for logging ALL HTTP request and responses -->
<!--<logger name="io.gatling.http.ahc" level="TRACE" />-->
<!--<logger name="io.gatling.http.response" level="TRACE" />-->
<!-- Uncomment for logging ONLY FAILED HTTP request and responses -->
<!--<logger name="io.gatling.http.ahc" level="DEBUG" />-->
<!--<logger name="io.gatling.http.response" level="DEBUG" />-->
If you want to print the response bodies to a file you could use the logback-file for this as well. I rather like using the following simple configuration, as it prints all the failing request/response logs to a file in the same catalogue as the gatling simulation results are stored, useful to see in more detail what kind of errors you get in your simulations in a dedicated file.
For Gatling 2.3:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
</encoder>
<immediateFlush>false</immediateFlush>
</appender>
<appender name="ERROR" class="ch.qos.logback.core.FileAppender">
<file>target/gatling/simulation-errors.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %logger{15} - %msg%n%rEx</pattern>
</encoder>
<immediateFlush>false</immediateFlush>
<param name="Append" value="false" />
</appender>
<logger name="io.gatling.http.ahc" level="DEBUG" additivity="false">
<appender-ref ref="ERROR"/>
</logger>
<logger name="io.gatling.http.response" level="DEBUG" additivity="false">
<appender-ref ref="ERROR"/>
</logger>
<root level="WARN">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
For Gatling 3.0, the two loggers above must be replaced with the following appender:
<logger name="io.gatling.http.engine.response" level="DEBUG" additivity="false">
<appender-ref ref="ERROR"/>
</logger>
val httpProtocol = http .baseURL(baseURL) .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36") .extraInfoExtractor(ExtraInfo => { println("httpCode: " + ExtraInfo.response.statusCode.getOrElse(0) + "\nResponse: " + ExtraInfo.response.body.string); List(ExtraInfo.response.statusCode, ExtraInfo.response.body.string) })
– Jacquline