An OkHttp interceptor which logs HTTP request and response data to an slf4j logger.
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
.build();
You can change the log level at any time on your corresponding logging configuration. For example, in log4j you'll use:
log4j.logger.okhttp3.logging.wire=DEBUG
See
HttpLoggingInterceptor
javadoc for the output description.
To log to a custom location, pass a Logger
instance to the constructor.
private static final Logger yourLogger = LoggerFactory.getLogger( YourClass.class );
...
HttpLoggingInterceptor logging = new HttpLoggingInterceptor( yourLogger );
Don't forget to update your logging properties as well!
Warning: The logs generated by this interceptor when using the DEBUG
or INFO
levels has the potential to leak sensitive information such as "Authorization" or "Cookie" headers and the contents of request and response bodies. This data should only be logged in a controlled way or in a non-production environment.
On your ~/.m2/settings.xml
, add:
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray-plugins</name>
<url>http://jcenter.bintray.com</url>
</pluginRepository>
</pluginRepositories>
<id>bintray</id>
</profile>
</profiles>
<activeProfiles>
<activeProfile>bintray</activeProfile>
</activeProfiles>
Get via Maven:
<dependency>
<groupId>devcsrj.okhttp3</groupId>
<artifactId>slf4j-logging-interceptor</artifactId>
<version>(insert latest version)</version>
</dependency>
or via Gradle
compile 'devcsrj.okhttp3:slf4j-logging-interceptor:(insert latest version)'