Ismael Di Vita Ismael Di Vita - 3 months ago 21
Android Question

Log method name and line number in Timber

Is possible log the current line number and method name using the Timber library

Expected LogCat result:

07-31 20:19:00.248 28998-28998/ismaeldivita.myapp I/[L:22] [M:onResume] [C:HomeActivity]: Praise the log!

Answer

Answering my own question.

Just create a new DebugTree class

public class MyDebugTree extends Timber.DebugTree {    
    @Override
    protected String createStackElementTag(StackTraceElement element) {
        return String.format("[L:%s] [M:%s] [C:%s]",
                element.getLineNumber(),
                element.getMethodName(),
                super.createStackElementTag(element));
    }
}

And plant your Tree in Timber:

public class App extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        if (BuildConfig.DEBUG) {
           Timber.plant(new MyDebugTree());
        } else {
           //TODO plant your Production Tree
        }
    }
}
Comments