Logging is one of the important tools for debugging an application.

When logging in Android, one usually writes code like

public class LoggingSample {

    public static final String TAG = "LoggingSample";
    ... 

    public void loggingMethod() {   

    final String formattedTestString = "first argument = %s, " + 
             "second argument = %s";
    final String firstArgument = "abc";
    final Object secondArgument = new  Object() {
        public String toString() {
            return "123";
        }
    };
    Log.d(TAG, "loggingMethod(): " + 
           String.format(formattedTestString, firstArgument,
           secondArgument));

    }
}

This then results in an output like

D/ LoggingSample: loggingMethod(): first argument = abc, second argument = 123

We need to add a tag and many times, in order to reproduce procedures, the name of the calling method. Not much of a deal, but since logging is done frequently it is often repeated

Since we don’t like repetitions, we decided to make it a little easier. Instead of the above, we now type

    L.d(formattedTestString, firstArgument, secondArgument);

To get an output like

D/LoggingSample:26(18510): onCreate(): first argument = abc, second argument = 123

What we get is the calling class’s name and the line number as a tag, the calling methods name plus the formatted string as a message.

 

If you are interested in this type of logging, you can now git clone the ANDLABS Android Utils and add them as a library project.

 

What do you think? How do you use logging? Please share your thoughts in the comments.