Android Logging Utility

Below is a snippet code for logging your application in logcat.

Usage:

Simply create a class called L.java and use it in your code.

public final class L {

    private static final String LOG_TAG = "ShopperMate_Log";

    private static final boolean DEBUG = true;

    public static void e(String message, Throwable cause) {
        if (DEBUG) {
            Log.e(LOG_TAG, "[" + message + "]", cause);
        }
    }

    public static void e(String msg) {
        if (DEBUG) {
            Log.e(LOG_TAG, "[" + getCallerClassName() + "] " + msg);
        }
    }

    public static void w(String message, Throwable cause) {
        if (DEBUG) {
            Log.w(LOG_TAG, "[" + message + "]", cause);
        }
    }

    public static void w(String msg) {
        if (DEBUG) {
            Log.w(LOG_TAG, "[" + getCallerClassName() + "] " + msg);
        }
    }

    public static void j(String title, Object msg) {
        if (DEBUG) {
            Log.w(LOG_TAG, "[" + getCallerClassName() + "] " + title + ": " + new Gson().toJson(msg));
        }
    }

    public static void i(String message, Throwable cause) {
        if (DEBUG) {
            Log.i(LOG_TAG, "[" + message + "]", cause);
        }
    }

    public static void i(String msg) {
        if (DEBUG) {
            Log.i(LOG_TAG, "[" + getCallerClassName() + "] " + msg);
        }
    }

    public static void i(String msg, Object... args) {
        if (DEBUG) {
            Log.i(LOG_TAG, "[" + getCallerClassName() + "] " + String.format(msg, args));
        }
    }

    public static void d(String msg, Throwable cause) {
        if (DEBUG) {
            Log.d(LOG_TAG, msg, cause);
        }
    }

    public static void d(String msg) {
        if (DEBUG) {
            Log.d(LOG_TAG, "[" + getCallerClassName() + "] " + msg);
        }
    }

    public static void v(String msg, Throwable cause) {
        if (DEBUG) {
            Log.v(LOG_TAG, msg, cause);
        }
    }

    public static void v(String msg) {
        if (DEBUG) {
            Log.v(LOG_TAG, "[" + getCallerClassName() + "] " + msg);
        }
    }

    public static void v(String format, Object... args) {
        if (DEBUG) {
            Log.v(LOG_TAG, "[" + getCallerClassName() + "] " + String.format(format, args));
        }
    }

    /**
     * Logging on steroids
     */
    public static void tag(String tag, String msg) {
        if (DEBUG) {

            Log.wtf(LOG_TAG,
                    "<" + getCallerClassName() + ">\n" +
                            "*******************************\n" +
                            "[" + tag + "]\n" +
                            msg);
        }
    }

    private static String getCallerClassName() {
        Throwable t = new Throwable();
        StackTraceElement[] elements = t.getStackTrace();

        return elements[1].getFileName();
    }
}

Toasting

public final class T {

    /**
     * Show custom toast
     */
    public static void show(Context context, String content) {
        View v = View.inflate(context.getApplicationContext(), R.layout.layout_toast, null);
        ((TextView)v.findViewById(R.id.text)).setText(content);
        Toast t = new Toast(context.getApplicationContext());
        t.setView(v);
        t.setGravity(Gravity.TOP,0,0);
        t.show();
    }

    /**
     * Show long duration toast
     */
    public static void showL(Context context, String content) {
        Toast.makeText(context, content, Toast.LENGTH_LONG).show();
    }

    /**
     * Show short duration toast
     */
    public static void showS(Context context, String content) {
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
    }

    /**
     * Showing network error toast
     */
    public static void showNetErr(Context context) {
        Toast.makeText(context, "404 Internet Not Found", Toast.LENGTH_SHORT).show();
    }

}

Snackbar

It's pretty straightforward you can even create a class for managing Snackbars.

public final class SB {

    /**
     * Show long duration Snackbar
     */
    public static void showL(View view, String content) {
         Snackbar.make(view, content, Snackbar.LENGTH_LONG).show();
    }

    /**
     * Show short duration Snackbar
     */
    public static void showS(View view, String content) {
         Snackbar.make(view, content, Snackbar.LENGTH_SHORT).show();
    }

}

Aiman Baharum

More about this blog https://github.com/aimanbaharum/random-wiki/wiki

Kuala Lumpur, Malaysia http://www.aimanbaharum.com

Subscribe to Knowledge Log

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!