自帶行號的log日誌提示
阿新 • • 發佈:2018-12-12
HELLO ,I MISS YOU!
使用環境:
需要在列印log的同時,提示在專案類之中的哪一行。
使用方法:
copy
public class LogUtil { private static boolean isDebug = false; // 是否在log中顯示行號 private static boolean SHOW_LINE_NUMBER_IN_LOG = isDebug; private static final String TAG = "com.sgcc.pda"; //是否已初始化 private static boolean initFlag = false; public static void init(boolean debugFlag) { isDebug = debugFlag; SHOW_LINE_NUMBER_IN_LOG = debugFlag; initFlag = true; } /** * 列印Log.w資訊 * * @param aPrintLogStr 列印資訊字串 */ public static void w(String aPrintLogStr) { LOG(TAG, aPrintLogStr, Log.WARN, false); } public static void w(String tag, String aPrintLogStr) { LOG(tag, aPrintLogStr, Log.WARN, false); } public static void i(String tag, String aPrintLogStr) { LOG(tag, aPrintLogStr, Log.INFO, false); } public static void i(String aPrintLogStr) { LOG(TAG, aPrintLogStr, Log.INFO, false); } public static void callerI(String aPrintLogStr) { LOG(TAG, aPrintLogStr, Log.INFO, true); } public static void d(String tag, String aPrintLogStr) { LOG(tag, aPrintLogStr, Log.DEBUG, false); } public static void d(String aPrintLogStr) { LOG(TAG, aPrintLogStr, Log.DEBUG, false); } public static void e(String tag, String aPrintLogStr) { LOG(tag, aPrintLogStr, Log.ERROR, false); } private static int LOG_MAXLENGTH = 2000; public static void e(String aPrintLogStr) { LOG(TAG, aPrintLogStr, Log.ERROR, false); } public static void v(String tag, String aPrintLogStr) { LOG(tag, aPrintLogStr, Log.VERBOSE, false); } /** * @param tag log的tag * @param content log的內容 * @param logType log的型別,如Log.INFO,Log.DEBUG等 * @param showCaller 是否顯示呼叫者的方法名和行號等 */ private static void LOG(String tag, String content, int logType, boolean showCaller) { if (!initFlag) { Log.e("LogUtil", "LogUtil沒有初始化,請先初始化後使用,如果未初始化Log將不會輸出顯示"); } if (isDebug) { if (SHOW_LINE_NUMBER_IN_LOG) { Throwable throwable = new Throwable(); //LogUtils.LOGI的depth是0, 該函式的depth是1, 呼叫者的depth是2,呼叫者的呼叫者是3 int methodDepth = 2; StackTraceElement[] stackTraceElements = throwable.getStackTrace(); if (methodDepth < stackTraceElements.length) { StackTraceElement element = stackTraceElements[methodDepth]; if (showCaller) { if ((methodDepth + 1) < stackTraceElements.length) { StackTraceElement elementCaller = stackTraceElements[methodDepth + 1]; if (elementCaller != null && elementCaller.getFileName() != null) { String callerClassName = elementCaller.getFileName().substring(0, elementCaller.getFileName().lastIndexOf(".")); String className = element.getFileName().substring(0, element.getFileName().lastIndexOf(".")); String loginfo = String.format("[%s.%s() (%s:%d)]", callerClassName, elementCaller.getMethodName(), elementCaller.getFileName(), elementCaller.getLineNumber()); loginfo = loginfo + "\n"; loginfo = loginfo + " " + String.format("[%s.%s() (%s:%d)]%s", className, element.getMethodName(), element.getFileName(), element.getLineNumber(), content); content = loginfo; } } } else { if (element != null && element.getFileName() != null) { String className = element.getFileName().substring(0, element.getFileName().lastIndexOf(".")); String loginfo = String.format("[%s.%s() (%s:%d)]%s", className, element.getMethodName(), element.getFileName(), element.getLineNumber(), content); content = loginfo; } } } } switch (logType) { case Log.INFO: Log.i(tag, "===================>" + content); break; case Log.DEBUG: Log.d(tag, "===================>" + content); break; case Log.ERROR: Log.e(tag, "===================>" + content); break; case Log.WARN: Log.w(tag, "===================>" + content); break; case Log.VERBOSE: Log.v(tag, "===================>" + content); break; default: break; } } } }
分享更多android小技巧,請進群:88627109