没有合适的资源?快使用搜索试试~ 我知道了~
首页android 日志文件LogUtils实例
背景 这是好久之前在网上找的一个常用类,已经忘记原文链接了,但是觉得很好用一直都在用,可以将日志写到file里面也可以定位你是在哪个类哪一行打印的日志,保存到文件的路径就是android/data/你的包名/files/目录下,然后我们就可以愉快的找问题了 import android.text.TextUtils; import android.util.Log; import com.smartlink.suixing.App; import com.smartlink.suixing.BuildConfig; import java.io.BufferedWriter; import
资源详情
资源评论
资源推荐

android 日志文件日志文件LogUtils实例实例
背景背景
这是好久之前在网上找的一个常用类,已经忘记原文链接了,但是觉得很好用一直都在用,可以将日志写到file里面也可以定
位你是在哪个类哪一行打印的日志,保存到文件的路径就是android/data/你的包名/files/目录下,然后我们就可以愉快的找问
题了
import android.text.TextUtils;
import android.util.Log;
import com.smartlink.suixing.App;
import com.smartlink.suixing.BuildConfig;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
public class LogUtils {
public static String customTagPrefix = "log"; // 自定义Tag的前缀,可以是作者名
private static final boolean isSaveLog = true; // 是否把保存日志到SD卡中
private static String cacheDirPath;
private LogUtils() {
}
// 容许打印日志的类型,默认是true,设置为false则不打印
public static boolean allowD = BuildConfig.DEBUG;
public static boolean allowE = BuildConfig.DEBUG;
public static boolean allowI = BuildConfig.DEBUG;
public static boolean allowV = BuildConfig.DEBUG;
public static boolean allowW = BuildConfig.DEBUG;
public static boolean allowWtf = BuildConfig.DEBUG;
// public static boolean allowD = true;
// public static boolean allowE = true;
// public static boolean allowI = true;
// public static boolean allowV = true;
// public static boolean allowW = true;
// public static boolean allowWtf = true;
private static String generateTag(StackTraceElement caller) {
String tag = "%s.%s(Line:%d)"; // 占位符
String callerClazzName = caller.getClassName(); // 获取到类名
callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber()); // 替换
tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
return tag;
}
/***
* 打印控制台显示不了那么长的日志问题
*
* @param msg
*/
public static void logE(String msg) { // 信息太长,分段打印
if (!allowE) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
// 因为String的length是字符数量不是字节数量所以为了防止中文字符过多,

// 把4*1024的MAX字节打印长度改为2001字符数
int max_str_length = 2001 - tag.length();
// 大于4000时
while (msg.length() > max_str_length) {
// Log.e(tag, msg.substring(0, max_str_length));
LogUtils.e(msg.substring(0, max_str_length));
msg = msg.substring(max_str_length);
}
// 剩余部分
// Log.e(tag, msg);
LogUtils.e(msg);
}
/**
* 自定义的logger
*/
public static CustomLogger customLogger;
public interface CustomLogger {
void d(String tag, String content);
void d(String tag, String content, Throwable tr);
void e(String tag, String content);
void e(String tag, String content, Throwable tr);
void i(String tag, String content);
void i(String tag, String content, Throwable tr);
void v(String tag, String content);
void v(String tag, String content, Throwable tr);
void w(String tag, String content);
void w(String tag, String content, Throwable tr);
void w(String tag, Throwable tr);
void wtf(String tag, String content);
void wtf(String tag, String content, Throwable tr);
void wtf(String tag, Throwable tr);
}
public static void d(String content) {
if (!allowD) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.d(tag, content);
} else {
Log.d(tag, content);
}
}
public static void d(String content, Throwable tr) {
if (!allowD) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.d(tag, content, tr);
} else {
Log.d(tag, content, tr);
}
}

public static void e(String content) {
if (!allowE) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.e(tag, content);
} else {
Log.e(tag, content);
}
if (isSaveLog) {
point(cacheDirPath, tag, content);
}
}
public static void e(String content, Throwable tr) {
if (!allowE) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.e(tag, content, tr);
} else {
Log.e(tag, content, tr);
}
if (isSaveLog) {
point(cacheDirPath, tag, tr.getMessage());
}
}
public static void e(Throwable tr) {
if (!allowE) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.e(tag, "", tr);
} else {
Log.e(tag, "", tr);
}
if (isSaveLog) {
point(cacheDirPath, tag, tr.getMessage());
}
}
public static void i(String content) {
if (!allowI) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.i(tag, content);
} else {
Log.i(tag, content);
}
}
public static void i(String content, Throwable tr) {
if (!allowI) return;
StackTraceElement caller = getCallerStackTraceElement();
String tag = generateTag(caller);
if (customLogger != null) {
customLogger.i(tag, content, tr);
} else {
Log.i(tag, content, tr);
}
}
剩余11页未读,继续阅读

weixin_38529397
- 粉丝: 4
- 资源: 939
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0