Skip to content

Commit

Permalink
Improving debug logging:
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dima-Android committed Aug 12, 2024
1 parent 27b6d93 commit e285f73
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 45 deletions.
33 changes: 17 additions & 16 deletions app/src/main/java/org/zotero/android/ZoteroApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/org/zotero/android/api/HttpLoggingInterceptor.kt
Original file line number Diff line number Diff line change
@@ -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 }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +24,6 @@ object WebDavModule {
@Singleton
@ForWebDav
fun provideWebDavOkHttpClient(
configuration: NetworkConfiguration,
webDavAuthNetworkInterceptor: WebDavAuthNetworkInterceptor,
): OkHttpClient {
val connectionPool = ConnectionPool(
Expand All @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e285f73

Please sign in to comment.