Skip to content

Commit 2f162d7

Browse files
committed
Timber with delegation
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.
1 parent 3f7f498 commit 2f162d7

File tree

9 files changed

+92
-16
lines changed

9 files changed

+92
-16
lines changed

LogcatCoreLib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
2323
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
2424
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
25-
api 'com.jakewharton.timber:timber:5.0.1'
25+
api 'com.github.hannesa2:timber:5.0.1.2@aar'
2626
}
2727

2828
project.afterEvaluate {

LogcatCoreLib/src/main/java/info/hannes/logcat/LoggingApplication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.app.Application
44
import info.hannes.timber.DebugFormatTree
55
import timber.log.Timber
66

7-
open class LoggingApplication : Application() {
7+
open class LoggingApplication(private val delegator: Class<*>? = null) : Application() {
88

99
override fun onCreate() {
1010
super.onCreate()
@@ -14,6 +14,6 @@ open class LoggingApplication : Application() {
1414
@Suppress("MemberVisibilityCanBePrivate")
1515
protected open fun setupLogging() {
1616
LoggingTools.globalErrorCatcher()
17-
Timber.plant(DebugFormatTree())
17+
Timber.plant(DebugFormatTree(delegator = delegator))
1818
}
1919
}

LogcatCoreLib/src/main/java/info/hannes/timber/DebugFormatTree.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.json.JSONException
44
import org.json.JSONObject
55
import timber.log.Timber
66

7-
open class DebugFormatTree : Timber.DebugTree() {
7+
open class DebugFormatTree(delegator: Class<*>? = null) : Timber.DebugTree(delegator = delegator) {
88

99
override fun createStackElementTag(element: StackTraceElement): String? {
1010
return String.format(
@@ -18,7 +18,7 @@ open class DebugFormatTree : Timber.DebugTree() {
1818
}
1919

2020
// if there is an JSON string, try to print out pretty
21-
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
21+
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
2222
var localMessage = message.trim()
2323
if (localMessage.startsWith("{") && localMessage.endsWith("}")) {
2424
try {
@@ -27,6 +27,6 @@ open class DebugFormatTree : Timber.DebugTree() {
2727
} catch (e: JSONException) {
2828
}
2929
}
30-
super.log(priority, tag, localMessage, t)
30+
super.logMessage(priority, tag, localMessage, t, *args)
3131
}
3232
}

LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
4646
}
4747

4848
@SuppressLint("LogNotTimber")
49-
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
49+
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
5050
try {
5151
val logTimeStamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(Date())
5252

@@ -84,7 +84,7 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
8484
}
8585
}
8686
// Don't call super, otherwise it logs twice
87-
//super.log(priority, tag, message, t)
87+
//super.logMessage(priority, tag, message, t, args)
8888
}
8989

9090
fun getFileName(): String = file.absolutePath

LogcatCountlyLib/src/main/java/info/hannes/timber/CountlyTree.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CountlyTree(private val analytics: Analytics, private val serverIgnoreToke
1919
private val t = serverIgnoreToken
2020
private val regex: Regex = "$t.+?$t|$t[^$t]*$".toRegex()
2121

22-
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
22+
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
2323
// we ignore INFO, DEBUG and VERBOSE
2424
if (priority <= Log.INFO) {
2525
return

LogcatCrashlyticLib/src/main/java/info/hannes/crashlytic/CrashlyticsTree.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicBoolean
88
@Suppress("unused")
99
class CrashlyticsTree(private val identifier: String? = null) : Timber.Tree() {
1010

11-
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
11+
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
1212
if (priority < Log.INFO) {
1313
return
1414
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package info.hannes.logcat
2+
3+
import timber.log.Timber
4+
5+
class CustomLogger : Logger {
6+
override fun v(message: String) {
7+
Timber.v(message)
8+
}
9+
10+
override fun v(t: Throwable, message: String) {
11+
Timber.v(t, message)
12+
}
13+
14+
override fun d(message: String) {
15+
Timber.d(message)
16+
}
17+
18+
override fun d(t: Throwable, message: String) {
19+
Timber.d(t, message)
20+
}
21+
22+
override fun i(message: String) {
23+
Timber.i(message)
24+
}
25+
26+
override fun i(t: Throwable, message: String) {
27+
Timber.i(t, message)
28+
}
29+
30+
override fun w(message: String) {
31+
Timber.w(message)
32+
}
33+
34+
override fun w(t: Throwable, message: String) {
35+
Timber.w(t, message)
36+
}
37+
38+
override fun e(message: String) {
39+
Timber.e(message)
40+
}
41+
42+
override fun e(t: Throwable, message: String) {
43+
Timber.e(t, message)
44+
}
45+
46+
override fun trace(owner: Any, message: String) {
47+
if (BuildConfig.DEBUG) {
48+
Timber.wtf(message)
49+
}
50+
}
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package info.hannes.logcat
2+
3+
import org.jetbrains.annotations.NonNls
4+
5+
interface Logger {
6+
fun v(@NonNls message: String)
7+
fun v(t: Throwable, @NonNls message: String)
8+
9+
fun d(@NonNls message: String)
10+
fun d(t: Throwable, @NonNls message: String)
11+
12+
fun i(@NonNls message: String)
13+
fun i(t: Throwable, @NonNls message: String)
14+
15+
fun w(@NonNls message: String)
16+
fun w(t: Throwable, @NonNls message: String)
17+
18+
fun e(@NonNls message: String)
19+
fun e(t: Throwable, @NonNls message: String)
20+
21+
fun trace(owner: Any, @NonNls message: String = "")
22+
}

sample/src/main/java/info/hannes/logcat/sample/CrashlyticApplication.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.os.*
66
import android.provider.Settings
77
import com.google.firebase.crashlytics.FirebaseCrashlytics
88
import info.hannes.crashlytic.CrashlyticsTree
9+
import info.hannes.logcat.CustomLogger
910
import info.hannes.logcat.LoggingApplication
1011
import info.hannes.timber.FileLoggingTree
1112
import kotlinx.coroutines.CoroutineScope
@@ -14,7 +15,9 @@ import kotlinx.coroutines.launch
1415
import timber.log.Timber
1516

1617

17-
class CrashlyticApplication : LoggingApplication() {
18+
class CrashlyticApplication : LoggingApplication(delegator = CustomLogger::class.java) {
19+
20+
private val logger = CustomLogger()
1821

1922
@SuppressLint("HardwareIds")
2023
override fun onCreate() {
@@ -39,15 +42,15 @@ class CrashlyticApplication : LoggingApplication() {
3942
FirebaseCrashlytics.getInstance().setCustomKey("VERSION_NAME", info.hannes.logcat.ui.BuildConfig.VERSIONNAME)
4043
Timber.plant(CrashlyticsTree(Settings.Secure.getString(applicationContext.contentResolver, Settings.Secure.ANDROID_ID)))
4144

42-
Timber.d("Debug test")
43-
Timber.i("Info test")
44-
Timber.w("Warning test")
45-
Timber.e("Error test")
45+
logger.d("Debug test")
46+
logger.i("Info test")
47+
logger.w("Warning test")
48+
logger.e("Error test")
4649

4750
var x = 0
4851
val runner: Runnable = object : Runnable {
4952
override fun run() {
50-
Timber.d("live=$x")
53+
logger.d("live=$x")
5154
x++
5255
Handler(Looper.getMainLooper()).postDelayed(this, 3000)
5356
}

0 commit comments

Comments
 (0)