public class LogUtils { public static String LOG_SWITCH = "android.app.all.log"; //log开关状态 0关 1开 public static String LOG_STATUS = "status"; @SuppressLint("StaticFieldLeak") private static Context sContext; private static LogSwitchReceiver sLogSwitchReceiver = new LogSwitchReceiver(); public static void init(Context context) { if (sContext != null){ return; } IntentFilter intentFilter = new IntentFilter(); //系统属性名称不可超过32字符,所以使用包名最后一段+.log 来做属性名 String[] strings = context.getPackageName().split("\."); intentFilter.addAction(strings[strings.length - 1] + ".log"); intentFilter.addAction(LOG_SWITCH); context.registerReceiver(new LogSwitchReceiver(), intentFilter); String allLog = SystemProperties.get(LOG_SWITCH); String thisLog = SystemProperties.get(strings[strings.length - 1] + ".log"); if ((!thisLog.isEmpty() && "1".equals(thisLog)) || (!allLog.isEmpty() && "1".equals(allLog))) { ULog.SWITCH = true; } sContext = context; } public static void release() { if (sContext != null && sLogSwitchReceiver != null) { sContext.unregisterReceiver(sLogSwitchReceiver); } } static class LogSwitchReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { int status = intent.getIntExtra(LOG_STATUS, 0); Log.d("LogUtils", "LogSwitchReceiver action: " + intent.getAction() + " status: " + status); SystemProperties.set(intent.getAction(), String.valueOf(status)); ULog.SWITCH = status == 1; } }}public class ULog { public static boolean SWITCH = BuildConfig.DEBUG; public static void v(String message) { if (SWITCH) { Log.v(getTag(), message); } } public static void d(String message) { if (SWITCH) { Log.d(getTag(), message); } } public static void i(String message) { if (SWITCH) { Log.i(getTag(), message); } } public static void w(String message) { if (SWITCH) { Log.w(getTag(), message); } } public static void e(String message) { if (SWITCH) { Log.e(getTag(), message); } } public static void getStackTraceString(Throwable tr) { if (SWITCH) { Log.getStackTraceString(tr); } } private static String getTag() { StackTraceElement[] traceElements = Thread.currentThread().getStackTrace(); String tag = null; if (traceElements != null && traceElements.length > 4) { StackTraceElement traceElement = traceElements[4]; tag = "(" + traceElement.getFileName() + ":" + traceElement.getLineNumber() + ")" + traceElement.getMethodName(); } else { tag = "ULog"; } return tag; } private static String generateLogcatText(String msg, int place, String message) { return generateLogcatText(Thread.currentThread().getStackTrace(), msg, place, message); } private static String generateLogcatText(StackTraceElement[] traceElements, String msg, int place, String message) { try { StringBuilder taskName = new StringBuilder(); if (traceElements != null && traceElements.length > place) { StackTraceElement traceElement = traceElements[place]; taskName.append(traceElement.getMethodName()).append("() : ").append(message); } return taskName.toString(); } catch (Throwable throwable) { return msg; } }}
推荐阅读
- 日志搜索,小功能大用处
- Android开发:使用Kotlin+协程+自定义注解+Retrofit的网络框架
- Android开发:当前项目以Module形式引用别的项目的步骤
- Android恶意木马伪装成游戏APP,通过华为AppGallery分发
- 2021年Android开发新技术动向,未来的路该怎么走?
- Uni-app离线打包Android APK详细教程
- 京东App秒级百G日志传输存储架构设计与实战
- 不会吧,你还不会用RequestId看日志 ?
- Windows系统服务器系统日志在哪里查看?
- Android 发布Library到Maven仓库