android打印日志封装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | public class LogUtils { static String className; //类名 static String methodName; //方法名 static int lineNumber; //行数 private LogUtils(){ /* Protect from instantiations */ } public static boolean isDebuggable() { return BuildConfig.DEBUG; } private static String createLog( String log ) { StringBuffer buffer = new StringBuffer(); buffer.append(methodName); buffer.append( "(" ).append(className).append( ":" ).append(lineNumber).append( ")" ); buffer.append(log); return buffer.toString(); } private static void getMethodNames(StackTraceElement[] sElements){ className = sElements[1].getFileName(); methodName = sElements[1].getMethodName(); lineNumber = sElements[1].getLineNumber(); } public static void e(String message){ if (!isDebuggable()) return ; // Throwable instance must be created before any methods getMethodNames( new Throwable().getStackTrace()); Log.e(className, createLog(message)); } public static void i(String message){ if (!isDebuggable()) return ; getMethodNames( new Throwable().getStackTrace()); Log.i(className, createLog(message)); } public static void d(String message){ if (!isDebuggable()) return ; getMethodNames( new Throwable().getStackTrace()); Log.d(className, createLog(message)); } public static void v(String message){ if (!isDebuggable()) return ; getMethodNames( new Throwable().getStackTrace()); Log.v(className, createLog(message)); } public static void w(String message){ if (!isDebuggable()) return ; getMethodNames( new Throwable().getStackTrace()); Log.w(className, createLog(message)); } public static void wtf(String message){ if (!isDebuggable()) return ; getMethodNames( new Throwable().getStackTrace()); Log.wtf(className, createLog(message)); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步