Skip to content

Commit ecd2e02

Browse files
committed
refactor(TrimeApplication.kt): refactor logging format
- Plant missing log tree in the release mode - Add current thread name in the log tag - Add current file name and line number in the log message in the debug mode
1 parent 1b68213 commit ecd2e02

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

app/src/main/java/com/osfans/trime/TrimeApplication.kt

+24-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.osfans.trime
22

33
import android.app.Application
44
import android.os.Process
5+
import android.util.Log
56
import androidx.preference.PreferenceManager
67
import cat.ereza.customactivityoncrash.config.CaocConfig
78
import com.osfans.trime.data.AppPrefs
@@ -37,13 +38,33 @@ class TrimeApplication : Application() {
3738
.apply()
3839
instance = this
3940
try {
40-
if (BuildConfig.DEBUG) {
41-
Timber.plant(Timber.DebugTree())
42-
}
4341
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
4442
AppPrefs.initDefault(sharedPreferences).apply {
4543
initDefaultPreferences()
4644
}
45+
if (BuildConfig.DEBUG) {
46+
Timber.plant(object : Timber.DebugTree() {
47+
override fun createStackElementTag(element: StackTraceElement): String {
48+
return "${super.createStackElementTag(element)}|${element.fileName}:${element.lineNumber}"
49+
}
50+
51+
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
52+
super.log(
53+
priority,
54+
"[${Thread.currentThread().name}] ${tag?.substringBefore('|')}",
55+
"${tag?.substringAfter('|')}] $message",
56+
t,
57+
)
58+
}
59+
})
60+
} else {
61+
Timber.plant(object : Timber.Tree() {
62+
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
63+
if (priority < Log.INFO) return
64+
Log.println(priority, "[${Thread.currentThread().name}]", message)
65+
}
66+
})
67+
}
4768
// record last pid for crash logs
4869
val appPrefs = AppPrefs.defaultInstance()
4970
val currentPid = Process.myPid()

0 commit comments

Comments
 (0)