From e285f734a6391d9e97ef187bcd5d9af50311af42 Mon Sep 17 00:00:00 2001 From: Dima Date: Mon, 12 Aug 2024 18:14:47 +0900 Subject: [PATCH] Improving debug logging: Enabling logging into a file for debug builds: Logging with BASIC level for Zotero APIs Logging with BODY level for WebDav and WebSockets Upping gradle to 8.9 Upping versionCode to 89 --- .../org/zotero/android/ZoteroApplication.kt | 33 ++++++++++--------- .../android/api/HttpLoggingInterceptor.kt | 13 ++++++++ .../api/module/ApiNoRedirectsModule.kt | 12 ++++--- .../android/api/module/ApiWebSocketModule.kt | 15 +++------ .../api/module/ApiWithAuthenticationModule.kt | 10 ++++-- .../android/api/module/BaseApiModule.kt | 4 --- .../zotero/android/api/module/WebDavModule.kt | 9 ++--- buildSrc/src/main/kotlin/BuildConfig.kt | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 9 files changed, 55 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/org/zotero/android/api/HttpLoggingInterceptor.kt diff --git a/app/src/main/java/org/zotero/android/ZoteroApplication.kt b/app/src/main/java/org/zotero/android/ZoteroApplication.kt index 66cb7490..569b16c3 100644 --- a/app/src/main/java/org/zotero/android/ZoteroApplication.kt +++ b/app/src/main/java/org/zotero/android/ZoteroApplication.kt @@ -98,23 +98,24 @@ open class ZoteroApplication: Application(), DefaultLifecycleObserver { } private fun setUpLogging() { - if (!EVENT_AND_CRASH_LOGGING_ENABLED) { - Timber.plant(object : Timber.DebugTree() { - override fun createStackElementTag(element: StackTraceElement): String { - return "[${Thread.currentThread().name}]" + - "${super.createStackElementTag(element)}" - } - }) - return + val consoleDebugTree = object : Timber.DebugTree() { + override fun createStackElementTag(element: StackTraceElement): String { + return "[${Thread.currentThread().name}]" + + "${super.createStackElementTag(element)}" + } } - val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() - Thread.setDefaultUncaughtExceptionHandler { thread, throwable -> - crashFileWriter.writeCrashToFile(throwable.stackTraceToString()) - defaultExceptionHandler?.uncaughtException( - thread, - throwable - ) + val listOfTrees = mutableListOf(consoleDebugTree, debugLoggingTree) + if (EVENT_AND_CRASH_LOGGING_ENABLED) { + val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() + Thread.setDefaultUncaughtExceptionHandler { thread, throwable -> + crashFileWriter.writeCrashToFile(throwable.stackTraceToString()) + defaultExceptionHandler?.uncaughtException( + thread, + throwable + ) + } + listOfTrees.add(FirebaseCrashReportingTree()) } - Timber.plant(FirebaseCrashReportingTree(), this.debugLoggingTree) + Timber.plant(*listOfTrees.toTypedArray()) } } \ No newline at end of file diff --git a/app/src/main/java/org/zotero/android/api/HttpLoggingInterceptor.kt b/app/src/main/java/org/zotero/android/api/HttpLoggingInterceptor.kt new file mode 100644 index 00000000..2df3c7aa --- /dev/null +++ b/app/src/main/java/org/zotero/android/api/HttpLoggingInterceptor.kt @@ -0,0 +1,13 @@ +package org.zotero.android.api + +import okhttp3.logging.HttpLoggingInterceptor +import okhttp3.logging.HttpLoggingInterceptor.Level +import timber.log.Timber + +object HttpLoggingInterceptor { + fun createInterceptor(logLevel: Level): HttpLoggingInterceptor { + return HttpLoggingInterceptor { message -> + Timber.d(message) + }.apply { level = logLevel } + } +} diff --git a/app/src/main/java/org/zotero/android/api/module/ApiNoRedirectsModule.kt b/app/src/main/java/org/zotero/android/api/module/ApiNoRedirectsModule.kt index 2dd0b3e8..3e525bc6 100644 --- a/app/src/main/java/org/zotero/android/api/module/ApiNoRedirectsModule.kt +++ b/app/src/main/java/org/zotero/android/api/module/ApiNoRedirectsModule.kt @@ -4,8 +4,10 @@ import dagger.Module import dagger.Provides import dagger.hilt.migration.DisableInstallInCheck import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor.Level import org.zotero.android.BuildConfig import org.zotero.android.api.AuthNetworkInterceptor +import org.zotero.android.api.HttpLoggingInterceptor import org.zotero.android.api.annotations.ForApiWithNoRedirects import org.zotero.android.api.annotations.ForBaseApi import retrofit2.Retrofit @@ -22,10 +24,12 @@ object ApiNoRedirectsModule { @ForBaseApi baseClient: OkHttpClient, authInterceptor: AuthNetworkInterceptor, ): OkHttpClient { - val clientBuilder = baseClient.newBuilder() - clientBuilder.followRedirects(false) - clientBuilder.addInterceptor(authInterceptor) - return clientBuilder.build() + return baseClient + .newBuilder() + .followRedirects(false) + .addInterceptor(authInterceptor) + .addInterceptor(HttpLoggingInterceptor.createInterceptor(Level.BASIC)) + .build() } @Provides diff --git a/app/src/main/java/org/zotero/android/api/module/ApiWebSocketModule.kt b/app/src/main/java/org/zotero/android/api/module/ApiWebSocketModule.kt index 9f99364e..d99a5640 100644 --- a/app/src/main/java/org/zotero/android/api/module/ApiWebSocketModule.kt +++ b/app/src/main/java/org/zotero/android/api/module/ApiWebSocketModule.kt @@ -4,11 +4,11 @@ import dagger.Module import dagger.Provides import dagger.hilt.migration.DisableInstallInCheck import okhttp3.OkHttpClient -import okhttp3.logging.HttpLoggingInterceptor -import org.zotero.android.api.annotations.ForWebSocket +import okhttp3.logging.HttpLoggingInterceptor.Level +import org.zotero.android.api.HttpLoggingInterceptor import org.zotero.android.api.NetworkConfiguration +import org.zotero.android.api.annotations.ForWebSocket import org.zotero.android.ktx.setNetworkTimeout -import timber.log.Timber import java.util.concurrent.TimeUnit import javax.inject.Singleton @@ -23,13 +23,8 @@ object ApiWebSocketModule { configuration: NetworkConfiguration ): OkHttpClient { return OkHttpClient.Builder() - .addInterceptor( - HttpLoggingInterceptor { message -> - Timber.tag("SvcSocket") - Timber.d(message) - } - .apply { level = HttpLoggingInterceptor.Level.BODY } - ).setNetworkTimeout(configuration.networkTimeout) + .addInterceptor(HttpLoggingInterceptor.createInterceptor(Level.BODY)) + .setNetworkTimeout(configuration.networkTimeout) .pingInterval(5, TimeUnit.SECONDS) .build() } diff --git a/app/src/main/java/org/zotero/android/api/module/ApiWithAuthenticationModule.kt b/app/src/main/java/org/zotero/android/api/module/ApiWithAuthenticationModule.kt index c14b3d6c..a6e45867 100644 --- a/app/src/main/java/org/zotero/android/api/module/ApiWithAuthenticationModule.kt +++ b/app/src/main/java/org/zotero/android/api/module/ApiWithAuthenticationModule.kt @@ -4,8 +4,10 @@ import dagger.Module import dagger.Provides import dagger.hilt.migration.DisableInstallInCheck import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor.Level import org.zotero.android.BuildConfig import org.zotero.android.api.AuthNetworkInterceptor +import org.zotero.android.api.HttpLoggingInterceptor import org.zotero.android.api.annotations.ForApiWithAuthentication import org.zotero.android.api.annotations.ForBaseApi import retrofit2.Retrofit @@ -45,8 +47,10 @@ object ApiWithAuthenticationModule { @ForBaseApi baseClient: OkHttpClient, authInterceptor: AuthNetworkInterceptor, ): OkHttpClient { - val clientBuilder = baseClient.newBuilder() - clientBuilder.addInterceptor(authInterceptor) - return clientBuilder.build() + return baseClient + .newBuilder() + .addInterceptor(authInterceptor) + .addInterceptor(HttpLoggingInterceptor.createInterceptor(Level.BASIC)) + .build() } } \ No newline at end of file diff --git a/app/src/main/java/org/zotero/android/api/module/BaseApiModule.kt b/app/src/main/java/org/zotero/android/api/module/BaseApiModule.kt index e9b1c740..5c807f7f 100644 --- a/app/src/main/java/org/zotero/android/api/module/BaseApiModule.kt +++ b/app/src/main/java/org/zotero/android/api/module/BaseApiModule.kt @@ -39,15 +39,11 @@ object BaseApiModule { dispatcher.maxRequests = 30 dispatcher.maxRequestsPerHost = 30 -// val interceptor = HttpLoggingInterceptor() -// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) - return OkHttpClient.Builder() .dispatcher(dispatcher) .connectionPool(connectionPool) .setNetworkTimeout(configuration.networkTimeout) .addInterceptor(clientInfoNetworkInterceptor) -// .addInterceptor(interceptor) .build() } diff --git a/app/src/main/java/org/zotero/android/api/module/WebDavModule.kt b/app/src/main/java/org/zotero/android/api/module/WebDavModule.kt index 7914700e..5467254e 100644 --- a/app/src/main/java/org/zotero/android/api/module/WebDavModule.kt +++ b/app/src/main/java/org/zotero/android/api/module/WebDavModule.kt @@ -6,7 +6,8 @@ import dagger.hilt.migration.DisableInstallInCheck import okhttp3.ConnectionPool import okhttp3.Dispatcher import okhttp3.OkHttpClient -import org.zotero.android.api.NetworkConfiguration +import okhttp3.logging.HttpLoggingInterceptor.Level +import org.zotero.android.api.HttpLoggingInterceptor import org.zotero.android.api.WebDavAuthNetworkInterceptor import org.zotero.android.api.annotations.ForWebDav import org.zotero.android.ktx.setNetworkTimeout @@ -23,7 +24,6 @@ object WebDavModule { @Singleton @ForWebDav fun provideWebDavOkHttpClient( - configuration: NetworkConfiguration, webDavAuthNetworkInterceptor: WebDavAuthNetworkInterceptor, ): OkHttpClient { val connectionPool = ConnectionPool( @@ -35,15 +35,12 @@ object WebDavModule { dispatcher.maxRequests = 30 dispatcher.maxRequestsPerHost = 30 -// val interceptor = HttpLoggingInterceptor() -// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY) - return OkHttpClient.Builder() .dispatcher(dispatcher) .connectionPool(connectionPool) .setNetworkTimeout(15L) .addInterceptor(webDavAuthNetworkInterceptor) -// .addInterceptor(interceptor) + .addInterceptor(HttpLoggingInterceptor.createInterceptor(Level.BODY)) .build() } diff --git a/buildSrc/src/main/kotlin/BuildConfig.kt b/buildSrc/src/main/kotlin/BuildConfig.kt index 5064b8c0..33c7564c 100644 --- a/buildSrc/src/main/kotlin/BuildConfig.kt +++ b/buildSrc/src/main/kotlin/BuildConfig.kt @@ -4,7 +4,7 @@ object BuildConfig { const val compileSdkVersion = 34 const val targetSdk = 34 - val versionCode = 88 // Must be updated on every build + val versionCode = 89 // Must be updated on every build val version = Version( major = 1, minor = 0, diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 33de4840..ad8926fa 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Mon Aug 15 17:35:12 GST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME