'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")


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arpit Shukla