Skip to content

Commit 96563f6

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 45d29f8 commit 96563f6

File tree

7 files changed

+88
-12
lines changed

7 files changed

+88
-12
lines changed

LogcatCoreLib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dependencies {
2424
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0"
2525
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.7.0"
2626
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.7.0'
27-
api 'com.github.hannesa2:timber:5.0.1.0'
27+
api 'com.github.hannesa2:timber:5.0.1.3'
2828
}
2929

3030
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import timber.log.Timber
66
import timber.log.Timber.Forest.tag
77

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

1111
private var codeIdentifier = ""
1212
private var method = ""
@@ -47,6 +47,6 @@ open class DebugFormatTree(private val newLogcat: Boolean = true) : Timber.Debug
4747
} catch (_: JSONException) {
4848
}
4949
}
50-
super.logMessage(priority, tag, "$method: $localMessage", t, args)
50+
super.logMessage(priority, tag, "$method: $localMessage", t, *args)
5151
}
5252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
8181
}
8282
}
8383
// Don't call super, otherwise it logs twice
84-
// super.log(priority, tag, message, t)
84+
// super.logMessage(priority, tag, message, t, args)
8585
}
8686

8787
fun getFileName(): String = file.absolutePath
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)