public class L {
public static final String TAG = "[TS]";
private static final int CALLER_LOCATION = 4;
private static final int MIN_STACK_TRACE_LENGTH = 5;
public static void i(String msg) {
String message = getCaller(msg);
Log.i(TAG, message);
}
private static String getCaller(String msg) {
StackTraceElement stackTrace[] = Thread.currentThread().getStackTrace();
if (stackTrace.length < MIN_STACK_TRACE_LENGTH) {
return msg;
}
StackTraceElement caller = stackTrace[CALLER_LOCATION];
return String.format("[%s]%s(%s:%d)", caller.getMethodName(), msg,
caller.getFileName(),
caller.getLineNumber());
}
}
输出样式:[TS]: [testRun]对这就是测试日志(Test.java:72)