public class ZLog {
private static final String TAG = "MyApplication";
// private static boolean isOpenD = Log.isLoggable(TAG, Log.DEBUG);
private static boolean isOpenD = true;
public static void d(String tag, String msg){
if(isOpenD){
StackTraceElement element = (new Exception()).getStackTrace()[1];
StringBuilder stringBuilder = new StringBuilder(" [ ")
.append(element.getFileName())
.append(" | ")
.append(element.getLineNumber())
.append(" | ")
.append(element.getMethodName() + "( )")
.append(" ] ");
Log.d(TAG +" : " + stringBuilder.toString() + tag, msg);
}
}
}
效果如下:
C:\Users\0016005615\cmder
λ adb shell logcat | grep MyApplication
10-28 16:28:23.673 11567 11567 D MyApplication : [ MainActivity.java | 41 | onCreate( ) ] MainActivity: Thread[main,5,main]
10-28 16:28:23.674 11567 11599 D MyApplication : [ MainActivity.java | 46 | run( ) ] MainActivity: 子線程
10-28 16:28:31.677 11567 11599 D MyApplication : [ MainActivity.java | 48 | run( ) ] MainActivity: Thread[Thread-3,5,main]