Monday, 23 January 2023

Timber - Problem with creating a custom Timber Debug Tree class

I'm trying to create log these values with a Custom DebugTree class, but I'm having problems getting it to work.

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'



from Timber - Problem with creating a custom Timber Debug Tree class

No comments:

Post a Comment