LoggingInterceptor - Interceptor for OkHttp3 with pretty logger
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();
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>
Add executor for allows to perform sequential concurrent print.
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("LoggingI") // Request & response each log tag
request("request") // Request log tag
response("response") // Response log tag
Header - Recipes
addHeader("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ") // Adding to request
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