Search code examples
androidtimber-android

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:

ismaeldivita.myapp I/[L:22] [M:onResume] [C:HomeActivity]: Praise the log!

Solution

  • 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
            }
        }
    }