OkHttpClient Logging Configuration With Interceptors

OkHttpClient Logging Configuration With Interceptors


Debugging HTTP can be very tricky. You have methods, headers, cookies, sessions, SSL, redirects, status codes and many other considerations. Making sure you set up proper logging for debugging purposes will allow you to track down issues much faster. The number one debugging tool for any HTTP client should without question be cURL. Once you are comfortable with cURL you can have absolute confidence you know a request should be working. Now you just need to track down the differences from cURL and your Java HTTP client of choice. Let’s take a look at logging with OkHttp.

OkHttp Interceptors

Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Basically, interceptors are equivalent to our Undertow Middleware. An interceptor is the perfect place for request/response logging in OkHttp. They even provide an excellent HttpLoggingInterceptor as an extra library. Simply set the level you want and provide your own logging mechanism to log the provided messages. We will be using SLF4J with Logback to create a static singleton HttpLoggingInterceptor.


via DZone.com Feed https://dzone.com

May 26, 2017 at 10:39AM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s