安卓开发中,经常需要打印日志,但是系统的日志,一是 需要频繁写 tag,但是tag经常使用同一个,二是,不能输出行数,三是,可能打印的日志很长,不能打印完全,所以需要一个简单的日志打印类,少写点代码。上代码:
import android.util.Log; /** * Created by more on 2017/12/7/007. */ public class LogUtils { public static void setDebugMode(boolean debugMode) { LogUtils.debugMode = debugMode; } public static boolean isDebugMode() { return debugMode; } static boolean debugMode = true; static String className;//类名 static String methodName;//方法名 static int lineNumber;//行数 private static String createLog(String log) { StringBuffer buffer = new StringBuffer(); buffer.append(methodName); buffer.append("(").append(className).append(":").append(lineNumber).append(")"); return buffer.toString(); } private static void getMethodNames(StackTraceElement[] sElements) { className = sElements[1].getFileName(); methodName = sElements[1].getMethodName(); lineNumber = sElements[1].getLineNumber(); } public static void logMe(String message) { if (debugMode) { Log.e("666", "| |"); logVeryLongLoge("666", "|" + message); getMethodNames(new Throwable().getStackTrace()); Log.e("666", "|" + createLog(message)); Log.e("666", "| |"); Log.e("666", "-----------------------------------------------------------------------"); Log.e("666", "| |"); } } public static void logMe999(String message) { if (debugMode) { Log.e("999", "| |"); Log.e("999", "|" + message); getMethodNames(new Throwable().getStackTrace()); Log.e("999", "|" + createLog(message)); Log.e("999", "| |"); Log.e("999", "-----------------------------------------------------------------------"); Log.e("999", "| |"); } } /** * 截断输出日志 * @param msg */ public static void logVeryLongLoge(String tag, String msg) { if (tag == null || tag.length() == 0 || msg == null || msg.length() == 0) return; int segmentSize = 3 * 1024; long length = msg.length(); if (length <= segmentSize ) {// 长度小于等于限制直接打印 Log.e(tag, msg); }else { while (msg.length() > segmentSize ) {// 循环分段打印日志 String logContent = msg.substring(0, segmentSize ); msg = msg.replace(logContent, ""); Log.e(tag, logContent); } Log.e(tag, msg);// 打印剩余日志 } } }
使用:
LogUtils.logMe("filePath :" + filePath);
参考文档:
- Android中Logcat日志打印不全解决办法 https://www.jianshu.com/p/9fcdda2d6b7d
- 超级Log工具,能显示你的文件名、方法、行数并且可以点击到那一行 https://blog.csdn.net/wenzhi20102321/article/details/78138399