An OkHttp interceptor which pretty logs request and response data.
Java
Clone or download
ihsanbal Merge pull request #48 from ihsanbal/development
 fix: duplicated header error regarding merge conflicts #47
Latest commit 68698b3 Jun 19, 2018

README.md

LoggingInterceptor - Interceptor for OkHttp3 with pretty logger

Build Status Android Arsenal API JAVA SwaggerUI

Usage

OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.addInterceptor(new LoggingInterceptor.Builder()
                .loggable(BuildConfig.DEBUG)
                .setLevel(Level.BASIC)
                .log(Platform.INFO)
                .request("Request")
                .response("Response")
                .addHeader("version", BuildConfig.VERSION_NAME)
                .addQueryParam("query", "0")
//              .logger(new Logger() {
//                  @Override
//                  public void log(int level, String tag, String msg) {
//                      Log.w(tag, msg);
//                  }
//              })
//              .executor(Executors.newSingleThreadExecutor())
               .build());
        OkHttpClient okHttpClient = client.build();

//You can use with Retrofit
Retrofit retrofitAdapter = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl("https://.../")
            .client(okHttpClient)
            .build();

Download

Gradle:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
	compile('com.github.ihsanbal:LoggingInterceptor:2.0.6') {
        	exclude group: 'org.json', module: 'json'
    	}
}

Maven:

<repository>
   <id>jitpack.io</id>
   <url>https://jitpack.io</url>
</repository>

<dependency>
	    <groupId>com.github.ihsanbal</groupId>
	    <artifactId>LoggingInterceptor</artifactId>
	    <version>2.0.6</version>
</dependency>

Executor

Add executor for allows to perform sequential concurrent print.

Level

setLevel(Level.BASIC)
	      .NONE // No logs
	      .BASIC // Logging url,method,headers and body.
	      .HEADERS // Logging headers
	      .BODY // Logging body

Platform - Platform

loggable(BuildConfig.DEBUG) // enable/disable sending logs output.
log(Platform.WARN) // setting log type

Tag

tag("LoggingI") // Request & response each log tag
request("request") // Request log tag
response("response") // Response log tag

Header - Recipes

addHeader("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ") // Adding to request

Notes

Some tips about log at this blog post: “The way to get faster on development.”

Also use the filter & configure logcat header for a better result