diff --git a/buildCommon/commonLibConfig.gradle b/buildCommon/commonLibConfig.gradle index ed75b5c..09f8a83 100644 --- a/buildCommon/commonLibConfig.gradle +++ b/buildCommon/commonLibConfig.gradle @@ -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}", diff --git a/commonLib/build.gradle b/commonLib/build.gradle index 02a1266..d1d49f2 100644 --- a/commonLib/build.gradle +++ b/commonLib/build.gradle @@ -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 // 添加retrofit转化gson能力依赖 diff --git a/commonLib/src/main/assets/baseUrl.properties b/commonLib/src/main/assets/baseUrl.properties index 55f0883..c8eead0 100644 --- a/commonLib/src/main/assets/baseUrl.properties +++ b/commonLib/src/main/assets/baseUrl.properties @@ -1,2 +1,2 @@ -debug1 = https://www.wanandroid +debug1 = https://www.wanandroid.com debug2 = https://www.baidu diff --git a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt index 6432715..f556638 100644 --- a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt +++ b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt @@ -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 { diff --git a/commonLib/src/main/java/com/common/commonlib/net/RequestManager.kt b/commonLib/src/main/java/com/common/commonlib/net/RequestManager.kt index 44727bc..654f8be 100644 --- a/commonLib/src/main/java/com/common/commonlib/net/RequestManager.kt +++ b/commonLib/src/main/java/com/common/commonlib/net/RequestManager.kt @@ -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 + } } \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/utils/BaseUtils.kt b/commonLib/src/main/java/com/common/commonlib/utils/BaseUtils.kt index 86fbec3..c61d72c 100644 --- a/commonLib/src/main/java/com/common/commonlib/utils/BaseUtils.kt +++ b/commonLib/src/main/java/com/common/commonlib/utils/BaseUtils.kt @@ -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() + } } \ No newline at end of file