Timber - Problem with creating a custom Timber Debug Tree class

440 views Asked by At

I'm trying to create a custom Debug Tree class to get the same result as the following:

enter image description here

enter image description here

I have followed this Stackoverflow answer:

Log method name and line number in Timber

But using that answer gives me two problems:

  1. Implementing the custom Debug Tree class does not log anything when I use more than one method.
public class MyDebugTree extends Timber.DebugTree {     
    @Override 
    protected String createStackElementTag(StackTraceElement element) {
        return String.format("(%s:%s)#%s",
            element.getFileName(),
            element.getLineNumber(),
            element.getMethodName());
    } 
} 
public class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        if (BuildConfig.DEBUG) {
            Timber.plant(new MyDebugTree);
        }
    }
}

The above causes it to not log at all.

If I use only return element.getFileName(); it successfully logs that one error.

  1. The second problem I'm having is that using a custom DebugTree class does not give me the same results as using err.getStackTrace()[0].getLineNumber().
 }, err -> {
     Timber.e("Method name: " + err);
     Timber.e("Method name: " + err.getStackTrace()[0].getMethodName());
}

enter image description here

The custom Debug Tree class does not display the name of the method I'm trying to log.

Why is it not logging when I use all three methods?

Also how can I get it to log like it would using

err.getStackTrace()[0].getMethodName()?


I'm using 'com.jakewharton.timber:timber:4.7.1'

1

There are 1 answers

0
Jian Astrero On

You seem to be doing it right. I recreated your code in kotlin (programming language should not matter) and i was able to show my logs.

MyDebugTree.kt

class QueenlyDebugTree : Timber.DebugTree() {
    override fun createStackElementTag(element: StackTraceElement): String {
        return "(${element.fileName}:${element.lineNumber})#${element.methodName}"
    }
}

force log using an actual exception:

try {
    throw RuntimeException("Hello World")
} catch (e: Exception) {
    Timber.e(e)
}

i got a log: enter image description here

So, from what i saw from your code, its most probably because you have a compact logcat view. To update your logcat view, follow these steps:

1. Make sure you have standard view selected enter image description here enter image description here

2. Configure standard view

enter image description here

3. Make sure Show tags is selected and tag column is at max(35) enter image description here