Tuesday, 31 July 2012

Convenient Logger for android

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]


            String className = fullClassName.substring(fullClassName

                    .lastIndexOf(".") + 1);

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


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


            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) {



        return sb.toString();




  1. Check out the logcat command. It allows to filter for any text part. There are customized variations available, e.g. ColoredLogcatPlusPlus. Just search for logcat on GitHub.

    1. Thanks,I just want to make things easy and simple.