Skip to content

Instantly share code, notes, and snippets.

@Mariovc
Created January 4, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mariovc/1643a750aa0e3da00f2c to your computer and use it in GitHub Desktop.
Save Mariovc/1643a750aa0e3da00f2c to your computer and use it in GitHub Desktop.
Android Log class improving the original class to do not log when it is on production and without tag needed
import com.antena3.floox.BuildConfig;
/**
* Author: Mario Velasco Casquero
* Date: 27/08/2015
*/
public class Log {
private static final int MAX_LOG_TAG_LENGTH = 23;
private static final boolean logsAllowed = BuildConfig.DEBUG;
public static void v(String tag, String message) {
if (logsAllowed) {
android.util.Log.v(tag, message);
}
}
public static void d(String tag, String message) {
if (logsAllowed) {
android.util.Log.d(tag, message);
}
}
public static void i(String tag, String message) {
if (logsAllowed) {
android.util.Log.i(tag, message);
}
}
public static void w(String tag, String message) {
if (logsAllowed) {
android.util.Log.w(tag, message);
}
}
public static void e(String tag, String message) {
if (logsAllowed) {
android.util.Log.e(tag, message);
}
}
public static void wtf(String tag, String message) {
if (logsAllowed) {
android.util.Log.wtf(tag, message);
}
}
public static void v(Object object, String message) {
if (logsAllowed) {
android.util.Log.v(getLogTag(object), message);
}
}
public static void d(Object object, String message) {
if (logsAllowed) {
android.util.Log.d(getLogTag(object), message);
}
}
public static void i(Object object, String message) {
if (logsAllowed) {
android.util.Log.i(getLogTag(object), message);
}
}
public static void w(Object object, String message) {
if (logsAllowed) {
android.util.Log.w(getLogTag(object), message);
}
}
public static void e(Object object, String message) {
if (logsAllowed) {
android.util.Log.e(getLogTag(object), message);
}
}
public static void wtf(Object object, String message) {
if (logsAllowed) {
android.util.Log.wtf(getLogTag(object), message);
}
}
public static void v(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.v(getLogTag(object), message, throwable);
}
}
public static void d(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.d(getLogTag(object), message, throwable);
}
}
public static void i(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.i(getLogTag(object), message, throwable);
}
}
public static void w(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.w(getLogTag(object), message, throwable);
}
}
public static void e(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.e(getLogTag(object), message, throwable);
}
}
public static void wtf(Object object, String message, Throwable throwable) {
if (logsAllowed) {
android.util.Log.wtf(getLogTag(object), message, throwable);
}
}
private static String getLogTag(Object object) {
String longName = object.getClass().getName();
String shortName = longName.substring(longName.lastIndexOf('.') + 1);
if (shortName.contains("$")) {
shortName = shortName.substring(0, shortName.indexOf('$'));
}
if (shortName.length() > MAX_LOG_TAG_LENGTH) {
shortName = shortName.substring(0, MAX_LOG_TAG_LENGTH - 1);
}
return shortName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment