Skip to content

Instantly share code, notes, and snippets.

@JMPergar
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JMPergar/7428e7a1fc26931ca7c3 to your computer and use it in GitHub Desktop.
Save JMPergar/7428e7a1fc26931ca7c3 to your computer and use it in GitHub Desktop.
LogManager.java
import android.util.Log;
public final class LogManager {
public static final int VERBOSE = 0;
public static final int DEBUG = 1;
public static final int INFO = 2;
public static final int WARN = 3;
public static final int ERROR = 4;
public static final int WTF = 5;
public static void log(int level, String className, String methodName, String msg) {
if (isLoggable(level)) {
String tag = generateTag(className, methodName);
switch (level) {
case VERBOSE: Log.v(tag, msg); break;
case DEBUG: Log.d(tag, msg); break;
case INFO: Log.i(tag, msg); break;
case WARN: Log.w(tag, msg); break;
case ERROR: Log.e(tag, msg); break;
case WTF: Log.w(tag, msg); break;
}
}
}
public static void log(int level, String className, String methodName, String msg, Throwable tr) {
if (isLoggable(level)) {
String tag = generateTag(className, methodName);
switch (level) {
case VERBOSE: Log.v(tag, msg, tr); break;
case DEBUG: Log.d(tag, msg, tr); break;
case INFO: Log.i(tag, msg, tr); break;
case WARN: Log.w(tag, msg, tr); break;
case ERROR: Log.e(tag, msg, tr); break;
case WTF: Log.w(tag, msg, tr); break;
}
}
}
private static boolean isLoggable (int level) {
return (BaseApplication.isDebuggable() || level > DEBUG);
}
private static String generateTag(String className, String methodName) {
return String.format("%s/%s", className, methodName);
}
}
public class BaseApplication extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
public static boolean isDebuggable() {
boolean isDebuggable = (0 != (getContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
return isDebuggable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment