参考 https://github.com/huangdali/ELog
https://github.com/ZhaoKaiQiang/KLog
package com.util.wlog; import android.os.Environment; import android.text.TextUtils; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; /** * 级别错误日志打印工具,带定位功能(输出调用处类型名、方法名、行,点击可追踪到) * Created by WM on 2017/7/24. */ public class WLog { /** * 是否是debug模式----总开关 */ private static boolean isDebug = true; /** * 用以区分多用户使用LOG */ private static final String TAG="WM"; /** * 打印错误信息 * * @param msg */ public static void e(Object msg) { if (!isDebug) { return; } Log.e(getTraceTAG(getStackTrace(),TAG), msg.toString()); } /** * 打印INFOR信息 * * @param msg */ public static void i(Object msg) { if (!isDebug) { return; } Log.i(getTraceTAG(getStackTrace(),TAG), msg.toString()); } /** * 打印DEBUG信息 * * @param msg */ public static void d(Object msg) { if (!isDebug) { return; } Log.d(getTraceTAG(getStackTrace(),TAG), msg.toString()); } /** * 打印错误信息 * * @param msg */ public static void file(String fileName, String msg) { if (!isDebug) { return; } printMsgToFile(getStackTrace(), fileName, msg); } /** * 获取StackTraceElement对象-----当前调用ELog.e()处的类信息(类名、方法名、行等) * * @return */ private static StackTraceElement getStackTrace() { return Thread.currentThread().getStackTrace()[4]; } /** * 设置是否打印e方法的日志 * * @param isDebug */ public static void setIsDebug(boolean isDebug) { WLog.isDebug = isDebug; } /** * 获取全部TAG信息 * @param element * @param tag * @return */ private static String getTraceTAG(StackTraceElement element, String tag){ StringBuilder sb = new StringBuilder(); String fileName = element.getFileName(); sb.append(element.getMethodName()) .append("(").append(fileName).append(":") .append(element.getLineNumber()) .append(")"); String lTag = (TextUtils.isEmpty(tag) ? "" : (tag + ":")) + sb.toString(); return lTag; } /** *打印jsonLog * @param msg */ public static void printJson( String msg) { String message; try { if (msg.startsWith("{")) { JSONObject jsonObject = new JSONObject(msg); message = jsonObject.toString(4); } else if (msg.startsWith("[")) { JSONArray jsonArray = new JSONArray(msg); message = jsonArray.toString(4); } else { message = msg; } } catch (JSONException e) { message = msg; } String[] lines = message.split("\n"); for (String line : lines) { d("║ " + line); } } /** * 打印日志到文件 * * @param element 当前调用WLog.e()处的类信息(类名、方法名、行等) * @param fileName * @param msg */ private static void printMsgToFile(StackTraceElement element, String fileName, String msg) { StringBuilder sb = new StringBuilder(); String className = element.getFileName(); sb.append(element.getMethodName()) .append(" (").append(className).append(":") .append(element.getLineNumber()) .append(") "); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS"); String content = "----------------------- " + format.format(new Date(System.currentTimeMillis())) + " -----------------------\n" + sb.toString() + "\n" + msg + "\n\n\n"; File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/WLog/"); if (!dir.exists()) { dir.mkdirs(); } FileOutputStream fos = null; try { fos = new FileOutputStream(dir.getPath() + "/" + fileName + ".txt", true); fos.write(content.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }