简介:loglibrary 是对Android Log进行了一层封装,主要实现了打印msg,且可以在Logcat页点击类名,跳转到相应的位置。
Log封装
package com.china.turing.custom.log;
import android.util.Log;
/**
* Copyright (C), 2021-2021
* FileName: LogUtils
* Author: ***
* Date: 8/2/21 4:18 PM
* Description: 日志工具类
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
public class LogUtils {
private static String className;
private static String methodName;
private static int lineNumber;
public static boolean isDebuggable() {
return BuildConfig.DEBUG;
}
private static String createLog(String log) {
StringBuffer buffer = new StringBuffer();
buffer.append("****************");
buffer.append(methodName);
buffer.append("(").append(className).append(":").append(lineNumber).append(")****************");
buffer.append(log);
return buffer.toString();
}
/**
* 获取文件名、方法名、所在行数
*
* @param sElements
*/
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;
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));
}
}
使用了Android studio生成的BuildConfig.DEBUG来控制日志是否打印,当打release包的时候,关闭日志打印。
android {
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
效果展示
2021-08-02 16:55:19.703 2456-19046/com.google.android.googlequicksearchbox I/AudioController: internalShutdown
2021-08-02 16:55:19.713 2456-2456/com.google.android.googlequicksearchbox I/MicroDetector: Keeping mic open: false
2021-08-02 16:55:19.713 2456-19045/com.google.android.googlequicksearchbox I/DeviceStateChecker: DeviceStateChecker cancelled
2021-08-02 16:55:19.713 2456-2456/com.google.android.googlequicksearchbox I/MicroDetectionWorker: #onError(false)
2021-08-02 16:55:19.925 1740-2054/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.face_recogition_demo01/.MainActivity bnds=[582,2043][857,2415] (has extras)} from uid 10022 on display 0
2021-08-02 16:55:19.930 1440-1466/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 12888890 , only wrote 12888720
2021-08-02 16:55:19.957 18991-18991/com.example.face_recogition_demo01 D/MainActivity.kt: ****************initView(MainActivity.kt:33)****************test
2021-08-02 16:55:19.967 1347-1406/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 14745600
2021-08-02 16:55:20.031 18991-19007/com.example.face_recogition_demo01 D/EGL_emulation: eglMakeCurrent: 0x9c517040: ver 3 0 (tinfo 0x9a66f6b0)
2021-08-02 16:55:20.043 2456-2456/com.google.android.googlequicksearchbox W/SearchService: Abort, client detached.
2021-08-02 16:55:20.064 2456-2717/com.google.android.googlequicksearchbox D/EGL_emulation: eglMakeCurrent: 0xa9d06e00: ver 3 0 (tinfo 0x87a54170)
2021-08-02 16:55:20.067 2509-2634/com.google.android.apps.nexuslauncher D/EGL_emulation: eglMakeCurrent: 0xa9d054e0: ver 3 0 (tinfo 0xa9d033b0)
2021-08-02 16:55:20.072 1740-1761/system_process I/ActivityManager: Displayed com.example.face_recogition_demo01/.MainActivity: +135ms
2021-08-02 16:55:20.463 1347-1347/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
2021-08-02 16:55:23.156 1440-1467/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 13196384 , only wrote 13043520
2021-08-02 16:55:48.836 1740-1846/system_process D/WifiNative-HAL: Failing getSupportedFeatureset because HAL isn't started