Convenient Logger for android

Read Time < 1 minute
Logging is an important technique of debugging during application development. Android provide Log class for logging different log-level messages. However in order to easily filter the log messages (in Logcat) and directly pinpointing the location of log message is a bit tricky (with default Log class).

I have implemented my own Log class primarily:

  • To set one application TAG (for ease of filtering).
  • To make logging enable/disable for development and release version.
  • and to print classname:function name:line no with log message like __FILE__,__FUNC__ and __LINE__ macros in C.
public class Log {



    private static boolean enableLog = true;

    private static String TAG = "YourAppTag";


public static void i(Object... objects) {

        if (enableLog) {

            android.util.Log.i(TAG, toString(objects));

        }

    }
public static void d(Object... objects) {

        if (enableLog) {



            String fullClassName = Thread.currentThread().getStackTrace()[3]

                    .getClassName();

            String className = fullClassName.substring(fullClassName

                    .lastIndexOf(".") + 1);

            String methodName = Thread.currentThread().getStackTrace()[3]

                    .getMethodName();

            int lineNumber = Thread.currentThread().getStackTrace()[3]

                    .getLineNumber();

            String finalMessage = className + "." + methodName + "():Line:"

                    + lineNumber + ":" + toString(objects);



            android.util.Log.d(TAG, finalMessage);

        }

    }



public static void e(Object... objects) {

        if (enableLog) {

            android.util.Log.e(TAG, toString(objects));

        }

    }

public static void w(Object... objects) {

        if (enableLog) {

            android.util.Log.w(TAG, toString(objects));

        }

    }


private static String toString(Object... objects) {

        StringBuilder sb = new StringBuilder();

        for (Object o : objects) {

            sb.append(o);

        }

        return sb.toString();

    }

}

2 thoughts on “Convenient Logger for android”

Leave a Comment

Your email address will not be published. Required fields are marked *