Last active
August 24, 2016 06:32
-
-
Save NayaneshGupte/357f3805ef12dcf3e831e2201e844b51 to your computer and use it in GitHub Desktop.
Debug Build Logger For Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DebugLogger.enableDebugLog(!BuildConfig.PRODUCTION_BUILD); | |
| if(!BuildConfig.DEBUG) { | |
| CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build(); | |
| Fabric.with(this, new Crashlytics.Builder().core(core).build()); | |
| DebugLogger.setErrorLogHandler(this); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DebugLogger { | |
| public static final String LOG_TAG = "DebugLogger"; | |
| private static boolean DEBUG = true; | |
| private static final boolean VERBOSE = true; | |
| private static ErrorLogHandler mErrLogHandler = null; | |
| public static void enableDebugLog(boolean enable){ | |
| DEBUG = enable; | |
| } | |
| public static void v(String msg) { | |
| if(VERBOSE) Log.v(LOG_TAG, buildMessage(msg)); | |
| } | |
| public static void d(String msg) { | |
| if(DEBUG) Log.d(LOG_TAG, buildMessage(msg)); | |
| } | |
| public static void e(String msg) { | |
| if(DEBUG) Log.e(LOG_TAG, buildMessage(msg)); | |
| } | |
| public static void e(String msg,Throwable tr) { | |
| Log.e(LOG_TAG, buildMessage(msg), tr); | |
| if(mErrLogHandler != null){ | |
| mErrLogHandler.onErrorLogged(msg,tr); | |
| } | |
| } | |
| public static void wtf(String msg) { | |
| Log.wtf(LOG_TAG, buildMessage(msg)); | |
| } | |
| public static void wtf(String msg, Throwable tr) { | |
| Log.wtf(LOG_TAG, buildMessage(msg), tr); | |
| } | |
| private static String buildMessage(String msg) { | |
| StackTraceElement[] trace = (new Throwable()).fillInStackTrace().getStackTrace(); | |
| String caller = "<unknown>"; | |
| for(int i = 2; i < trace.length; ++i) { | |
| Class clazz = trace[i].getClass(); | |
| if(!clazz.equals(DebugLogger.class)) { | |
| String callingClass = trace[i].getClassName(); | |
| callingClass = callingClass.substring(callingClass.lastIndexOf(46) + 1); | |
| callingClass = callingClass.substring(callingClass.lastIndexOf(36) + 1); | |
| caller = callingClass + "." + trace[i].getMethodName()+":"+trace[i].getLineNumber(); | |
| break; | |
| } | |
| } | |
| return String.format(Locale.US, "[%d] %s: %s", new Object[]{Long.valueOf(Thread.currentThread().getId()), caller, msg}); | |
| } | |
| public static void i(String s) { | |
| if(VERBOSE) Log.i(LOG_TAG, buildMessage(s)); | |
| } | |
| public static void setErrorLogHandler(ErrorLogHandler handler) { | |
| mErrLogHandler = handler; | |
| } | |
| public interface ErrorLogHandler{ | |
| void onErrorLogged(String msg,Throwable t); | |
| } | |
| public static boolean isLogEnabled() { | |
| return DEBUG; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment