From 2f162d783803d15547bc6746ba63c32a4acc585f Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Mon, 12 Dec 2022 04:07:24 +0100 Subject: [PATCH] Timber with delegation It uses a modifier Timber, which allows to use a delegation logger This reverts commit 4894135e14282e72c6601732f4941fdf1689470f. Revert "Revert "Demonstrate how to use a delegate class"" This reverts commit 379aa5bb8852b2d2d132199ebf6e769a677ec67a. --- LogcatCoreLib/build.gradle | 2 +- .../info/hannes/logcat/LoggingApplication.kt | 4 +- .../info/hannes/timber/DebugFormatTree.kt | 6 +-- .../info/hannes/timber/FileLoggingTree.kt | 4 +- .../java/info/hannes/timber/CountlyTree.kt | 2 +- .../info/hannes/crashlytic/CrashlyticsTree.kt | 2 +- .../java/info/hannes/logcat/CustomLogger.kt | 51 +++++++++++++++++++ .../main/java/info/hannes/logcat/Logger.kt | 22 ++++++++ .../logcat/sample/CrashlyticApplication.kt | 15 +++--- 9 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 sample/src/main/java/info/hannes/logcat/CustomLogger.kt create mode 100644 sample/src/main/java/info/hannes/logcat/Logger.kt diff --git a/LogcatCoreLib/build.gradle b/LogcatCoreLib/build.gradle index d3da8810..35deccb3 100644 --- a/LogcatCoreLib/build.gradle +++ b/LogcatCoreLib/build.gradle @@ -22,7 +22,7 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1" implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1' - api 'com.jakewharton.timber:timber:5.0.1' + api 'com.github.hannesa2:timber:5.0.1.2@aar' } project.afterEvaluate { diff --git a/LogcatCoreLib/src/main/java/info/hannes/logcat/LoggingApplication.kt b/LogcatCoreLib/src/main/java/info/hannes/logcat/LoggingApplication.kt index 97040a59..cdd350d3 100644 --- a/LogcatCoreLib/src/main/java/info/hannes/logcat/LoggingApplication.kt +++ b/LogcatCoreLib/src/main/java/info/hannes/logcat/LoggingApplication.kt @@ -4,7 +4,7 @@ import android.app.Application import info.hannes.timber.DebugFormatTree import timber.log.Timber -open class LoggingApplication : Application() { +open class LoggingApplication(private val delegator: Class<*>? = null) : Application() { override fun onCreate() { super.onCreate() @@ -14,6 +14,6 @@ open class LoggingApplication : Application() { @Suppress("MemberVisibilityCanBePrivate") protected open fun setupLogging() { LoggingTools.globalErrorCatcher() - Timber.plant(DebugFormatTree()) + Timber.plant(DebugFormatTree(delegator = delegator)) } } diff --git a/LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt b/LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt index 9c15ea0b..14d218eb 100644 --- a/LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt +++ b/LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt @@ -4,7 +4,7 @@ import org.json.JSONException import org.json.JSONObject import timber.log.Timber -open class DebugFormatTree : Timber.DebugTree() { +open class DebugFormatTree(delegator: Class<*>? = null) : Timber.DebugTree(delegator = delegator) { override fun createStackElementTag(element: StackTraceElement): String? { return String.format( @@ -18,7 +18,7 @@ open class DebugFormatTree : Timber.DebugTree() { } // if there is an JSON string, try to print out pretty - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { + override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) { var localMessage = message.trim() if (localMessage.startsWith("{") && localMessage.endsWith("}")) { try { @@ -27,6 +27,6 @@ open class DebugFormatTree : Timber.DebugTree() { } catch (e: JSONException) { } } - super.log(priority, tag, localMessage, t) + super.logMessage(priority, tag, localMessage, t, *args) } } diff --git a/LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt b/LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt index 7a9deaa9..76e85b12 100644 --- a/LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt +++ b/LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt @@ -46,7 +46,7 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil } @SuppressLint("LogNotTimber") - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { + override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) { try { val logTimeStamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(Date()) @@ -84,7 +84,7 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil } } // Don't call super, otherwise it logs twice - //super.log(priority, tag, message, t) + //super.logMessage(priority, tag, message, t, args) } fun getFileName(): String = file.absolutePath diff --git a/LogcatCountlyLib/src/main/java/info/hannes/timber/CountlyTree.kt b/LogcatCountlyLib/src/main/java/info/hannes/timber/CountlyTree.kt index 70180632..7e946512 100644 --- a/LogcatCountlyLib/src/main/java/info/hannes/timber/CountlyTree.kt +++ b/LogcatCountlyLib/src/main/java/info/hannes/timber/CountlyTree.kt @@ -19,7 +19,7 @@ class CountlyTree(private val analytics: Analytics, private val serverIgnoreToke private val t = serverIgnoreToken private val regex: Regex = "$t.+?$t|$t[^$t]*$".toRegex() - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { + override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) { // we ignore INFO, DEBUG and VERBOSE if (priority <= Log.INFO) { return diff --git a/LogcatCrashlyticLib/src/main/java/info/hannes/crashlytic/CrashlyticsTree.kt b/LogcatCrashlyticLib/src/main/java/info/hannes/crashlytic/CrashlyticsTree.kt index bc126dac..918b33f4 100644 --- a/LogcatCrashlyticLib/src/main/java/info/hannes/crashlytic/CrashlyticsTree.kt +++ b/LogcatCrashlyticLib/src/main/java/info/hannes/crashlytic/CrashlyticsTree.kt @@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicBoolean @Suppress("unused") class CrashlyticsTree(private val identifier: String? = null) : Timber.Tree() { - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { + override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) { if (priority < Log.INFO) { return } diff --git a/sample/src/main/java/info/hannes/logcat/CustomLogger.kt b/sample/src/main/java/info/hannes/logcat/CustomLogger.kt new file mode 100644 index 00000000..85e3d6df --- /dev/null +++ b/sample/src/main/java/info/hannes/logcat/CustomLogger.kt @@ -0,0 +1,51 @@ +package info.hannes.logcat + +import timber.log.Timber + +class CustomLogger : Logger { + override fun v(message: String) { + Timber.v(message) + } + + override fun v(t: Throwable, message: String) { + Timber.v(t, message) + } + + override fun d(message: String) { + Timber.d(message) + } + + override fun d(t: Throwable, message: String) { + Timber.d(t, message) + } + + override fun i(message: String) { + Timber.i(message) + } + + override fun i(t: Throwable, message: String) { + Timber.i(t, message) + } + + override fun w(message: String) { + Timber.w(message) + } + + override fun w(t: Throwable, message: String) { + Timber.w(t, message) + } + + override fun e(message: String) { + Timber.e(message) + } + + override fun e(t: Throwable, message: String) { + Timber.e(t, message) + } + + override fun trace(owner: Any, message: String) { + if (BuildConfig.DEBUG) { + Timber.wtf(message) + } + } +} diff --git a/sample/src/main/java/info/hannes/logcat/Logger.kt b/sample/src/main/java/info/hannes/logcat/Logger.kt new file mode 100644 index 00000000..2b61c659 --- /dev/null +++ b/sample/src/main/java/info/hannes/logcat/Logger.kt @@ -0,0 +1,22 @@ +package info.hannes.logcat + +import org.jetbrains.annotations.NonNls + +interface Logger { + fun v(@NonNls message: String) + fun v(t: Throwable, @NonNls message: String) + + fun d(@NonNls message: String) + fun d(t: Throwable, @NonNls message: String) + + fun i(@NonNls message: String) + fun i(t: Throwable, @NonNls message: String) + + fun w(@NonNls message: String) + fun w(t: Throwable, @NonNls message: String) + + fun e(@NonNls message: String) + fun e(t: Throwable, @NonNls message: String) + + fun trace(owner: Any, @NonNls message: String = "") +} diff --git a/sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt b/sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt index bc0a8e8f..fc55aa06 100644 --- a/sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt +++ b/sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt @@ -6,6 +6,7 @@ import android.os.* import android.provider.Settings import com.google.firebase.crashlytics.FirebaseCrashlytics import info.hannes.crashlytic.CrashlyticsTree +import info.hannes.logcat.CustomLogger import info.hannes.logcat.LoggingApplication import info.hannes.timber.FileLoggingTree import kotlinx.coroutines.CoroutineScope @@ -14,7 +15,9 @@ import kotlinx.coroutines.launch import timber.log.Timber -class CrashlyticApplication : LoggingApplication() { +class CrashlyticApplication : LoggingApplication(delegator = CustomLogger::class.java) { + + private val logger = CustomLogger() @SuppressLint("HardwareIds") override fun onCreate() { @@ -39,15 +42,15 @@ class CrashlyticApplication : LoggingApplication() { FirebaseCrashlytics.getInstance().setCustomKey("VERSION_NAME", info.hannes.logcat.ui.BuildConfig.VERSIONNAME) Timber.plant(CrashlyticsTree(Settings.Secure.getString(applicationContext.contentResolver, Settings.Secure.ANDROID_ID))) - Timber.d("Debug test") - Timber.i("Info test") - Timber.w("Warning test") - Timber.e("Error test") + logger.d("Debug test") + logger.i("Info test") + logger.w("Warning test") + logger.e("Error test") var x = 0 val runner: Runnable = object : Runnable { override fun run() { - Timber.d("live=$x") + logger.d("live=$x") x++ Handler(Looper.getMainLooper()).postDelayed(this, 3000) }