ahmad
11/24/2016 - 9:52 AM

Simple extension of android Logcat class which makes easy to change debugging mode.

Simple extension of android Logcat class which makes easy to change debugging mode.


import android.util.Log;

public class Logcat{

	/*
	 * ALLOW_LOG to enable or disable debugging log
	 */
	private static final boolean ALLOW_LOG = true;

	
	/*
	 * @since API level 1
	 * 
	 * Send an DEBUG log message.
	 */
	public static void e(String tag, String message){
		if(ALLOW_LOG){
			Log.e(tag, message);
		}
	}
	
	/*
	 * @since API level 1
	 * 
	 * Send an DEBUG log message.
	 * 
	 */
	
	public static void d(String tag, String message){
		if(ALLOW_LOG){
			Log.d(tag, message);
		}
	}
	
	/*
	 * @since API level 1
	 * 
	 * Send an VERBOSE log message.
	 */
	public static void v(String tag, String message){
		if(ALLOW_LOG){
			Log.v(tag, message);
		}
	}
	
	
	/*
	 * @since API level 1
	 * 
	 * Send an INFO log message.
	 */
	public static void i(String tag, String message){
		if(ALLOW_LOG){
			Log.i(tag, message);
		}
	}
	
	/*
	 * @since API level 1
	 * 
	 * Send a WARN log message.
	 */
	public static void w(String tag, String message){
		if(ALLOW_LOG){
			Log.w(tag, message);
		}
	}
	

	/*
	 * @since API level 8 
	 * 
	 * What a Terrible Failure: Report a condition that should never happen.
	 * The error will always be logged at level ASSERT with the call stack.
	 * Depending on system configuration, a report may be added to the DropBoxManager
	 * and/or the process may be terminated immediately with an error dialog.
	 */
	public static void wtf(String tag, String message){
		if(ALLOW_LOG){
			Log.wtf(tag, message);
		}
	}

	// Show long message in android studio 
	public static void l(String tag, String message) {
	     if(ALLOW_LOG){
	     	if(message != null){
			int maxLogSize = 1000;
		        int length = message.length() / maxLogSize;
		        for(int i = 0; i <= length; i++) {
		            int start = i * maxLogSize;
		            int end = (i+1) * maxLogSize;
		            end = end > message.length() ? message.length() : end;
		            Log.e(tag, message.substring(start, end));
		        }		    		     		
	     	}
	     }
    	}


}