https://github.com/ihsanbal/LoggingInterceptor
  
  
    An OkHttp interceptor which has pretty logger for request and response. +Mock support 
    https://github.com/ihsanbal/LoggingInterceptor
  
android interceptor java java-library kotlin logging-library logginginterceptor mock mocking-framework okhttp
        Last synced: 3 months ago 
        JSON representation
    
An OkHttp interceptor which has pretty logger for request and response. +Mock support
- Host: GitHub
 - URL: https://github.com/ihsanbal/LoggingInterceptor
 - Owner: ihsanbal
 - License: mit
 - Created: 2017-02-22T17:56:10.000Z (over 8 years ago)
 - Default Branch: master
 - Last Pushed: 2024-02-13T14:21:04.000Z (over 1 year ago)
 - Last Synced: 2024-11-21T03:51:49.587Z (12 months ago)
 - Topics: android, interceptor, java, java-library, kotlin, logging-library, logginginterceptor, mock, mocking-framework, okhttp
 - Language: Kotlin
 - Homepage:
 - Size: 32.6 MB
 - Stars: 1,326
 - Watchers: 30
 - Forks: 112
 - Open Issues: 1
 - 
            Metadata Files:
            
- Readme: README.md
 - License: LICENSE
 
 
Awesome Lists containing this project
- awesome-android - LoggingInterceptor - An OkHttp interceptor which pretty logs request and response data. (Libraries / Logger)
 - awesome-android-libraries - LoggingInterceptor
 - awesome-android-libraries - LoggingInterceptor
 - awesome-kotlin - LoggingInterceptor - An OkHttp interceptor which has pretty logger for request and response. +Mock support (Libraries)
 - awesome-android - LoggingInterceptor - An OkHttp interceptor which pretty logs request and response data. (Libraries / Logger)
 - awesome-android-libraries - LoggingInterceptor
 - awesome-java - Logging Interceptor
 
README
          LoggingInterceptor - Interceptor for [OkHttp3](https://github.com/square/okhttp) with pretty logger
--------
[](https://travis-ci.org/ihsanbal/LoggingInterceptor)
[](http://androidweekly.net/issues/issue-272)
[](https://android-arsenal.com/details/1/5870)
[](http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html)
[](https://jitpack.io/#ihsanbal/LoggingInterceptor)
[](https://www.mockable.io/swagger/index.html?url=https%3A%2F%2Fdemo2961085.mockable.io%3Fopenapi#!/demo2961085)
    
Usage
--------
```kotlin
val client = OkHttpClient.Builder()
    client.addInterceptor(LoggingInterceptor.Builder()
             .setLevel(Level.BASIC)
             .log(VERBOSE)
             .addHeader("cityCode","53")
             .addQueryParam("moonStatus", "crescent")
             .build())
```
Download
--------
Gradle:
Groovy
```groovy
allprojects {
	repositories {
		maven { url 'https://jitpack.io' }
	}
}
dependencies {
	implementation('com.github.ihsanbal:LoggingInterceptor:3.1.0') {
        	exclude group: 'org.json', module: 'json'
    	}
}
```
kotlin DSL
```
allprojects {
	repositories {
		maven { setUrl("https://jitpack.io") }
	}
}
dependencies {
	implementation("com.github.ihsanbal:LoggingInterceptor:3.1.0") {
        	exclude(group = "org.json", module = "json")
    	}
}
```
Maven:
```xml
   jitpack.io
   https://jitpack.io
    com.github.ihsanbal
    LoggingInterceptor
    3.1.0
```
Logger & Mock Support
---------------------
```kotlin
LoggingInterceptor.Builder()
    //Add logger to print log as plain text
    .logger(object : Logger {
          override fun log(level: Int, tag: String?, msg: String?) {
              Log.e("$tag - $level", "$msg")
          }
      })
      //Enable mock for develop app with mock data
      .enableMock(BuildConfig.MOCK, 1000L, object : BufferListener {
          override fun getJsonResponse(request: Request?): String? {
              val segment = request?.url?.pathSegments?.getOrNull(0)
              return mAssetManager.open(String.format("mock/%s.json", segment)).source().buffer().readUtf8()
          }
      })
```	
Level
--------
```kotlin
setLevel(Level.BASIC)
	      .NONE // No logs
	      .BASIC // Logging url,method,headers and body.
	      .HEADERS // Logging headers
	      .BODY // Logging body
```	
Platform - [Platform](https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/platform/Platform.java)
--------
```kotlin
log(Platform.WARN) // setting log type
```
Tag
--------
```kotlin
tag("LoggingI") // Request & response each log tag
request("request") // Request log tag
response("response") // Response log tag
```
	
Header - [Recipes](https://github.com/square/okhttp/wiki/Recipes)
--------
```kotlin
addHeader("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ") // Adding to request
```
Notes
--------
Some tips about log at this blog post: [“The way to get faster on development.”](https://medium.com/@ihsanbal/the-way-to-get-faster-on-development-9d7b23ef8c10)
Also use the filter & configure logcat header for a better result