只有在debug下才打印日志
public class Logger {
private String tag;
public Logger(String tag) {
super();
this.tag = tag;
}
public Logger(Object object) {
this(object.getClass());
}
public Logger(Class<?> clazz) {
this(clazz.getSimpleName());
}
public void d(String format, Object... args) {
if (BuildConfig.DEBUG)
Log.d(tag, String.format(format, args));
}
public void e(String format, Object... args) {
if (BuildConfig.DEBUG)
Log.e(tag, String.format(format, args));
}
public void e(Throwable throwable, String format, Object... args) {
if (BuildConfig.DEBUG)
Log.e(tag, String.format(format, args), throwable);
}
public void i(String format, Object... args) {
if (BuildConfig.DEBUG) {
String msg = String.format(format, args);
// 日志太长 分段打印
int count = msg.length() / 3600 + 1;
for (int i = 0; i < count; i++) {
int index = i * 3600;
if (i == count - 1) {
if (i == 0) {
Log.i(tag, msg);
} else {
Log.i(tag, msg.substring(index));
}
} else {
Log.i(tag, msg.substring(index, index + 3600));
}
}
}
}
public void w(String format, Object... args) {
if (BuildConfig.DEBUG)
Log.w(tag, String.format(format, args));
}
}
本文介绍了一个用于Android应用开发的日志记录类Logger。该类在调试模式下支持不同类型的日志输出,包括debug、error、info等,并针对过长日志进行分段打印。

被折叠的 条评论
为什么被折叠?



