返回排行榜

ihsanbal/LoggingInterceptor

Kotlin

An OkHttp interceptor which has pretty logger for request and response. +Mock support

androidokhttplogginginterceptorlogging-libraryjavajava-libraryinterceptormockmocking-frameworkkotlin
Star 增长趋势
Star
1.4k
Forks
121
周增长
Issues
0
5001k
2023年1月2024年3月2025年5月2026年7月
README

LoggingInterceptor - Interceptor for OkHttp3 with pretty logger

Android Arsenal API SwaggerUI

Usage

val client = OkHttpClient.Builder()
    client.addInterceptor(LoggingInterceptor.Builder()
             .setLevel(Level.BASIC)
             .log(VERBOSE)
             .addHeader("cityCode","53")
             .addQueryParam("moonStatus", "crescent")
             .build())

Download

Gradle:

Groovy

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

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

kotlin DSL

allprojects {
	repositories {
		maven { setUrl("https://jitpack.io") }
	}
}


dependencies {
    implementation("com.github.ihsanbal:LoggingInterceptor:4.0.0") {
            exclude(group = "org.json", module = "json")
        }
}

Batching and custom sinks (fork feature)

This fork adds a sink(...) API so you can batch a whole request/response block before logging to avoid interleaving in Logcat. Example (using the bundled BatchingSink, now public):

val sink = BatchingSink(LogSink { type, tag, message ->
    // Logcat truncates ~4k per line; forward to your own chunker if needed
    Log.println(type, tag, message)
})

val client = OkHttpClient.Builder()
    .addInterceptor(
        LoggingInterceptor.Builder()
            .setLevel(Level.BODY)
            .log(Log.DEBUG)
            .sink(sink)
            .build()
    )
    .build()

If you need chunking/queuing (e.g., Logcat 4k limit), wrap the `LogSink` to your own queue before passing to `BatchingSink`, similar to the sample above.

If you want the forked artifact via JitPack:

allprojects {
    repositories { maven { setUrl("https://jitpack.io") } }
}

dependencies {
    implementation("com.github.rtsketo:LoggingInterceptor:3.1.0rt6") {
        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>4.0.0</version>
</dependency>

Logger & Mock Support

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

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

相关仓库
flutter/flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond

DartBSD 3-Clause "New" or "Revised" Licensemobileandroid
flutter.dev
177.9k30.7k
Genymobile/scrcpy

Display and control your Android device

CApache License 2.0androidc
146.1k13.5k
react/react-native

A framework for building native applications using React

C++MIT Licenseandroidapp-framework
reactnative.dev
126.2k25.2k
facebook/react-native

A framework for building native applications using React

C++MIT Licenseandroidapp-framework
reactnative.dev
120.9k24.5k
rustdesk/rustdesk

An open-source remote desktop application designed for self-hosting, as an alternative to TeamViewer.

Rustcrates.ioGNU Affero General Public License v3.0remote-controlremote-desktop
rustdesk.com
118.6k18.1k
justjavac/free-programming-books-zh_CN

:books: 免费的计算机编程类中文书籍,欢迎投稿

GNU General Public License v3.0pythonjavascript
weibo.com/justjavac
117.7k28.2k
Hack-with-Github/Awesome-Hacking

A collection of various awesome lists for hackers, pentesters and security researchers

Creative Commons Zero v1.0 Universalhackingsecurity
116.6k10.5k
tldr-pages/tldr

Collaborative cheatsheets for console commands 📚.

MarkdownOthershellman-page
tldr.sh
63.2k5.3k
Solido/awesome-flutter

An awesome list that curates the best Flutter libraries, tools, tutorials, articles and more.

Dartflutterawesome-list
60.7k6.9k
2dust/v2rayNG

A V2Ray client for Android, support Xray core and v2fly core

KotlinGNU General Public License v3.0androidproxy
v2rayng.2dust.link
59.9k7.8k
termux/termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.

JavaMavenOtherandroidterminal
f-droid.org/en/packages/com.termux
57.9k7k
wasabeef/awesome-android-ui

A curated list of awesome Android UI/UX libraries

MIT Licenseandroidawesome
56.9k10.3k