Skip to content

Commit

Permalink
Timber with delegation
Browse files Browse the repository at this point in the history
It uses a modifier Timber, which allows to use a delegation logger

This reverts commit 4894135.

Revert "Revert "Demonstrate how to use a delegate class""
This reverts commit 379aa5b.
  • Loading branch information
hannesa2 committed Dec 12, 2022
1 parent 3f7f498 commit 2f162d7
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LogcatCoreLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -14,6 +14,6 @@ open class LoggingApplication : Application() {
@Suppress("MemberVisibilityCanBePrivate")
protected open fun setupLogging() {
LoggingTools.globalErrorCatcher()
Timber.plant(DebugFormatTree())
Timber.plant(DebugFormatTree(delegator = delegator))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
51 changes: 51 additions & 0 deletions sample/src/main/java/info/hannes/logcat/CustomLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
22 changes: 22 additions & 0 deletions sample/src/main/java/info/hannes/logcat/Logger.kt
Original file line number Diff line number Diff line change
@@ -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 = "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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)
}
Expand Down

0 comments on commit 2f162d7

Please sign in to comment.