Skip to content

Instantly share code, notes, and snippets.

@Bencodes
Created August 13, 2013 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bencodes/6226187 to your computer and use it in GitHub Desktop.
Save Bencodes/6226187 to your computer and use it in GitHub Desktop.
andorid.util.Log wrapper that automatically grabs tags from the stack trace and adds string formatting in.
public final class Log {
public static void d (final String format, final Object... params) {
if (BuildConfig.DEBUG) {
android.util.Log.d(getTagFromStackTrace(), String.format(format, params));
}
}
public static void d (final Throwable e) {
if (BuildConfig.DEBUG) {
android.util.Log.d(getTagFromStackTrace(), "", e);
}
}
public static void e (final String format, final Object... params) {
if (BuildConfig.DEBUG) {
android.util.Log.e(getTagFromStackTrace(), String.format(format, params));
}
}
public static void e (final Throwable e) {
if (BuildConfig.DEBUG) {
android.util.Log.e(getTagFromStackTrace(), "", e);
}
}
public static void i (final String format, final Object... params) {
if (BuildConfig.DEBUG) {
android.util.Log.i(getTagFromStackTrace(), String.format(format, params));
}
}
public static void i (final Throwable e) {
if (BuildConfig.DEBUG) {
android.util.Log.i(getTagFromStackTrace(), "", e);
}
}
public static void w (final Throwable e) {
if (BuildConfig.DEBUG) {
android.util.Log.w(getTagFromStackTrace(), e);
}
}
public static void w (final String format, final Object... params) {
if (BuildConfig.DEBUG) {
android.util.Log.w(getTagFromStackTrace(), String.format(format, params));
}
}
private static final String getTagFromStackTrace () {
// Find the calling class
return Thread.currentThread().getStackTrace()[4].getFileName().replace(".java", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment