实现一个可定位、可点击的Logcat日志打印工具类
程序员文章站
2022-03-09 16:16:13
...
实现一个可定位、可点击的Logcat日志打印工具类
前言:最近发现项目中的使用的Log太多,经常不知道Log日志是在哪个地方备注的。想从日志中看又太麻烦。于是整理此类,方便项目中使用。
效果如下:
在Android Studio的Logcat栏中可直观看到日志在哪个类的哪个方法中。点击后跳转到Log日志位置。
备注:Android Studio定位问题导致定位位置总是比Log日志位置多三行……(目前不知道是哪里出现了什么问题)
代码如下:
package com.lcz.wanandroid_kotlin.utils;
import android.util.Log;
/**
* @author 承泽
* @date 2020/8/25 13:55
* @Description:Log工具类
*/
public class LogUtils {
private static String getSimpleClassName(String name) {
int lastIndex = name.lastIndexOf(".");
return name.substring(lastIndex + 1);
}
public static void i(String msg) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int methodCount = 1;
int stackOffset = getStackOffset(trace);
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
StackTraceElement element = trace[stackIndex];
StringBuilder builder = new StringBuilder();
builder.append(getSimpleClassName(element.getClassName()))
.append(".")
.append(element.getMethodName())
.append(" ")
.append(" (")
.append(element.getFileName())
.append(":")
.append(element.getLineNumber())
.append(")")
.append("打印信息:----->")
.append(msg);
Log.i("LCZLOG_I", builder.toString());
}
}
public static void v(String msg) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int methodCount = 1;
int stackOffset = getStackOffset(trace);
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
StackTraceElement element = trace[stackIndex];
StringBuilder builder = new StringBuilder();
builder.append(getSimpleClassName(element.getClassName()))
.append(".")
.append(element.getMethodName())
.append(" ")
.append(" (")
.append(element.getFileName())
.append(":")
.append(element.getLineNumber())
.append(")")
.append("运行日志:----->")
.append(msg);
Log.v("LCZLOG_V", builder.toString());
}
}
public static void w(String msg) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int methodCount = 1;
int stackOffset = getStackOffset(trace);
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
StackTraceElement element = trace[stackIndex];
StringBuilder builder = new StringBuilder();
builder.append(getSimpleClassName(element.getClassName()))
.append(".")
.append(element.getMethodName())
.append(" ")
.append(" (")
.append(element.getFileName())
.append(":")
.append(element.getLineNumber())
.append(")")
.append("打印日志:----->")
.append(msg);
Log.w("LCZLOG_W", builder.toString());
}
}
public static void e(String msg) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int methodCount = 1;
int stackOffset = getStackOffset(trace);
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
StackTraceElement element = trace[stackIndex];
StringBuilder builder = new StringBuilder();
builder.append(getSimpleClassName(element.getClassName()))
.append(".")
.append(element.getMethodName())
.append(" ")
.append(" (")
.append(element.getFileName())
.append(":")
.append(element.getLineNumber())
.append(")")
.append("出现Error:----->")
.append(msg);
Log.e("LCZLOG_E", builder.toString());
}
}
public static void d(String msg) {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
int methodCount = 1;
int stackOffset = getStackOffset(trace);
if (methodCount + stackOffset > trace.length) {
methodCount = trace.length - stackOffset - 1;
}
for (int i = methodCount; i > 0; i--) {
int stackIndex = i + stackOffset;
if (stackIndex >= trace.length) {
continue;
}
StackTraceElement element = trace[stackIndex];
StringBuilder builder = new StringBuilder();
builder.append(getSimpleClassName(element.getClassName()))
.append(".")
.append(element.getMethodName())
.append(" ")
.append(" (")
.append(element.getFileName())
.append(":")
.append(element.getLineNumber())
.append(")")
.append("debug日志:----->")
.append(msg);
Log.d("LCZLOG_D", builder.toString());
}
}
private static int getStackOffset(StackTraceElement[] trace) {
for (int i = 2; i < trace.length; i++) {
StackTraceElement e = trace[i];
String name = e.getClassName();
if (!name.equals(LogUtils.class.getName())) {
return --i;
}
}
return -1;
}
}
以上即为效果图实现方式,各位可参考使用!