简体   繁体   中英

Android Studio: Timber: automatically add the function name

Does anyone know a trick to include the function name in a Timber text/tag without actually having to type it? Here's what I do and I figured I might as well ask... (initRecognizer is the function's name; ideally I enter something like $xyz) Thank you

Timber.d("SR: initRecognizer psid=$psid")

By default, Timber uses className as the tag. You can provide your own tag by overriding createStackElementTag function while planting the tree. Something like:

Timber.plant(
    object : Timber.DebugTree() {
        override fun createStackElementTag(element: StackTraceElement): String {
            val className = super.createStackElementTag(element)
            return "TAG $className ${element.methodName}"
        }
    }
)

I usually use a "TAG" prefix in my tags to quickly filter my logs from logcat.

Now you simply call Timber.d("your debug msg") and the function name will automatically be added in the tag in the logcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM