Skip to content

Commit 2c180f2

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 f4ebc16 commit 2c180f2

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
@@ -26,7 +26,7 @@ dependencies {
2626
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
2727
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.6.1"
2828
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
29-
api 'com.jakewharton.timber:timber:5.0.1'
29+
api 'com.github.hannesa2:timber:5.0.1.2@aar'
3030
}
3131

3232
publishing {

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
@@ -5,7 +5,7 @@ import org.json.JSONObject
55
import timber.log.Timber
66

77
/* If you use old logcat, e.g Android Studio Electric Eel, you should use for newLogcat a false */
8-
open class DebugFormatTree(private val newLogcat: Boolean = true) : Timber.DebugTree() {
8+
open class DebugFormatTree(delegator: Class<*>? = null, private val newLogcat: Boolean = true) : Timber.DebugTree(delegator) {
99

1010
private var codeIdentifier = ""
1111

@@ -34,7 +34,7 @@ open class DebugFormatTree(private val newLogcat: Boolean = true) : Timber.Debug
3434
}
3535

3636
// if there is an JSON string, try to print out pretty
37-
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
37+
override fun logMessage(priority: Int, tag: String?, message: String, t: Throwable?, vararg args: Any?) {
3838
var localMessage = message.trim()
3939
if (localMessage.startsWith("{") && localMessage.endsWith("}")) {
4040
try {
@@ -45,6 +45,6 @@ open class DebugFormatTree(private val newLogcat: Boolean = true) : Timber.Debug
4545
}
4646
if (newLogcat)
4747
localMessage = codeIdentifier + localMessage
48-
super.log(priority, tag, localMessage, t)
48+
super.logMessage(priority, tag, localMessage, t, *args)
4949
}
5050
}

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() {
@@ -43,15 +46,15 @@ class CrashlyticApplication : LoggingApplication() {
4346
} else
4447
Timber.w("No valid Firebase key was given")
4548

46-
Timber.d("Debug test")
47-
Timber.i("Info test")
48-
Timber.w("Warning test")
49-
Timber.e("Error test")
49+
logger.d("Debug test")
50+
logger.i("Info test")
51+
logger.w("Warning test")
52+
logger.e("Error test")
5053

5154
var x = 0
5255
val runner: Runnable = object : Runnable {
5356
override fun run() {
54-
Timber.d("live=$x")
57+
logger.d("live=$x")
5558
x++
5659
Handler(Looper.getMainLooper()).postDelayed(this, 3000)
5760
}

0 commit comments

Comments
 (0)