[desc]:添加OKHTTP日志监听

[author]:wangyimiao
master
yimiao 3 years ago
parent 29db3ecd46
commit 9241427370

@ -39,6 +39,7 @@ project.ext {
gson : "2.8.6",
arouter : "1.5.2",
mmkv : "1.2.10",
okhttp_logger : "3.12.7",
]
dependencies = [
@ -46,6 +47,7 @@ project.ext {
retrofit : "com.squareup.retrofit2:retrofit:${versions.retrofit}",
rxjava : "io.reactivex.rxjava3:rxjava:${versions.rxjava}",
rxandroid : "io.reactivex.rxjava3:rxandroid:${versions.rxandroid}",
okhttp_logger : "com.squareup.okhttp3:logging-interceptor:${versions.okhttp_logger}",
kotlin : "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}",
kotlin_android : "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.kotlin_android}",
converter_gson : "com.squareup.retrofit2:converter-gson:${versions.converter_gson}",

@ -28,10 +28,12 @@ dependencies {
implementation(rootProject.ext.dependencies.mmkv)
// kotlin
implementation rootProject.ext.dependencies.kotlin
implementation rootProject.ext.dependencies.kotlin_android
implementation(rootProject.ext.dependencies.kotlin)
implementation(rootProject.ext.dependencies.kotlin_android)
// okhttp
implementation rootProject.ext.dependencies.okhttp
implementation(rootProject.ext.dependencies.okhttp)
// okhttp
implementation(rootProject.ext.dependencies.okhttp_logger)
// retrofit
implementation rootProject.ext.dependencies.retrofit
// retrofitgson

@ -1,2 +1,2 @@
debug1 = https://www.wanandroid
debug1 = https://www.wanandroid.com
debug2 = https://www.baidu

@ -3,7 +3,10 @@ package com.common.commonlib
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import com.common.commonlib.log.Logger.d
import com.common.commonlib.utils.BaseUtils
import com.tencent.mmkv.MMKV
import okhttp3.logging.HttpLoggingInterceptor
/**
* 基础Application
@ -23,6 +26,12 @@ open class CommonApplication : Application() {
// 初始化MMKV
MMKV.initialize(this)
initNet()
}
private fun initNet() {
BaseUtils.enableFullLog()
}
companion object {

@ -1,9 +1,11 @@
package com.common.commonlib.net
import com.common.commonlib.log.Logger.d
import com.common.commonlib.net.interceptor.BaseUrlInterceptor
import com.common.commonlib.utils.BaseUtils
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
@ -16,6 +18,9 @@ import java.util.concurrent.TimeUnit
* @since 2021/7/28
*/
object RequestManager {
const val TAG = "RequestManager"
private var logLevel = HttpLoggingInterceptor.Level.NONE
/**
* 默认超时时间(s)
*/
@ -30,6 +35,10 @@ object RequestManager {
private var mRetrofitWithIntercept: Retrofit? = null
private val loggingInterceptor = HttpLoggingInterceptor {
d(TAG, it)
}
/**
* 创建service
*/
@ -55,6 +64,10 @@ object RequestManager {
// 动态HOST替换拦截器
builder.addInterceptor(BaseUrlInterceptor())
// 添加日志拦截器
loggingInterceptor.level = logLevel
builder.addInterceptor(loggingInterceptor)
if (!BaseUtils.isListEmpty(interceptors)) {
for (intercept in interceptors!!) {
builder.addInterceptor(intercept)
@ -74,4 +87,25 @@ object RequestManager {
.build()
}
}
/**
* 打开完整日志
*/
fun openFullLog() {
logLevel = HttpLoggingInterceptor.Level.BODY
}
/**
* 打开基础日志
*/
fun openBasicLog() {
logLevel = HttpLoggingInterceptor.Level.BASIC
}
/**
* 打开头部日志
*/
fun openHeadLog() {
logLevel = HttpLoggingInterceptor.Level.HEADERS
}
}

@ -2,6 +2,7 @@ package com.common.commonlib.utils
import android.content.Context
import android.os.Environment
import com.common.commonlib.net.RequestManager
/**
* 基础工具类
@ -23,4 +24,25 @@ object BaseUtils {
fun getExternalStorageDirectory(context: Context): String? {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path
}
/**
* 打开网络请求日志
*/
fun enableFullLog() {
RequestManager.openFullLog()
}
/**
* 打开网络请求基础日志
*/
fun enableBasicLog() {
RequestManager.openBasicLog()
}
/**
* 打开网络请求日志
*/
fun enableHeadersLog() {
RequestManager.openHeadLog()
}
}
Loading…
Cancel
Save