您好,登錄后才能下訂單哦!
背景
這是好久之前在網(wǎng)上找的一個常用類,已經(jīng)忘記原文鏈接了,但是覺得很好用一直都在用,可以將日志寫到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是字符數(shù)量不是字節(jié)數(shù)量所以為了防止中文字符過多, // 把4*1024的MAX字節(jié)打印長度改為2001字符數(shù) 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); } } public static void v(String content) { if (!allowV) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.v(tag, content); } else { Log.v(tag, content); } } public static void v(String content, Throwable tr) { if (!allowV) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.v(tag, content, tr); } else { Log.v(tag, content, tr); } } public static void w(String content) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, content); } else { Log.w(tag, content); } } public static void w(String content, Throwable tr) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, content, tr); } else { Log.w(tag, content, tr); } } public static void w(Throwable tr) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, tr); } else { Log.w(tag, tr); } } public static void wtf(String content) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, content); } else { Log.wtf(tag, content); } } public static void wtf(String content, Throwable tr) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, content, tr); } else { Log.wtf(tag, content, tr); } } public static void wtf(Throwable tr) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, tr); } else { Log.wtf(tag, tr); } } private static StackTraceElement getCallerStackTraceElement() { return Thread.currentThread().getStackTrace()[4]; } public static void point(String path, String tag, String msg) { if (isSDAva()) { path = cacheDirPath; Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE); dateFormat.applyPattern("yyyy"); path = path + dateFormat.format(date) + "/"; dateFormat.applyPattern("MM"); path += dateFormat.format(date) + "/"; dateFormat.applyPattern("dd"); path += dateFormat.format(date) + ".log"; dateFormat.applyPattern("[yyyy-MM-dd HH:mm:ss]"); String time = dateFormat.format(date); File file = new File(path); if (!file.exists()) createDipPath(path); BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); out.write(time + " " + tag + " " + msg + "\r\n"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } } /** * 根據(jù)文件路徑 遞歸創(chuàng)建文件 * * @param file */ public static void createDipPath(String file) { String parentFile = file.substring(0, file.lastIndexOf("/")); File file1 = new File(file); File parent = new File(parentFile); if (!file1.exists()) { parent.mkdirs(); try { file1.createNewFile(); LogUtils.e("日志文件的路徑是" + file1.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } } /** * A little trick to reuse a formatter in the same thread */ private static class ReusableFormatter { private Formatter formatter; private StringBuilder builder; public ReusableFormatter() { builder = new StringBuilder(); formatter = new Formatter(builder); } public String format(String msg, Object... args) { formatter.format(msg, args); String s = builder.toString(); builder.setLength(0); return s; } } private static final ThreadLocal<ReusableFormatter> thread_local_formatter = new ThreadLocal<ReusableFormatter>() { protected ReusableFormatter initialValue() { return new ReusableFormatter(); } }; public static String format(String msg, Object... args) { ReusableFormatter formatter = thread_local_formatter.get(); return formatter.format(msg, args); } public static boolean isSDAva() { if (cacheDirPath == null) cacheDirPath = App.getAppContext().getExternalFilesDir("log").getAbsolutePath(); if (!TextUtils.isEmpty(cacheDirPath)) { return true; } else { return false; } } }
補充知識:Android日志打印類LogUtils,能夠定位到類名,方法名以及出現(xiàn)錯誤的行數(shù)并保存日志文件
在開發(fā)中,我們常常用打印log的方式來調(diào)試我們的應用。在Java中我們常常使用方法System.out.println()來在控制臺打印日志,以便我們的調(diào)試。Android中有一個專門的類Log來實現(xiàn)在Android系統(tǒng)下日志的打印,更加方便我們定位程序出現(xiàn)問題的地方。
但是Android官方提供的Log類在實際項目使用中,也不是非常方便。當程序出現(xiàn)錯誤時,我們最希望的就是這個Log類能幫我們定位到是哪個類的哪個方法,甚至于是那一行出現(xiàn)了錯誤。這樣就能給我們的調(diào)試帶來很大的便利?! ?/p>
同時我們也應該想到為了應用程序的安全起見,在app正式上線之前,我們應該要把打印日志的功能關閉掉,以防別人通過Log來破解你的應用。生產(chǎn)模式的下打印Log,正式模式就把打印日志的功能關閉掉,要做到Log的靈活關閉與打開,也需要在原生的Log類上進行一些封裝?! ?/p>
還有一種時候,當我們的程序出現(xiàn)問題崩潰了,我們希望能夠收集到出現(xiàn)異常的原因進行分析,所以可以把Log日志保存到一個文件中,放在SD卡程序創(chuàng)建的目錄下。也可以在用戶聯(lián)網(wǎng)的情況下,在程序的后臺把出異常的Log日志文件上傳到服務端,方便程序員進行分析,解決bug。
今天就給大家分享一個做項目中很實用的一個Log類LogUtils,這個類是從xUtils中提取出來,稍作修改的,有注釋?! ?/p>
示例:
我們在MainActivity中調(diào)用一些LogUtils中的方法,注意看行數(shù)?! ?1
接著看看控制臺打印的日志是不是像MainActivity調(diào)用的那樣,Log中有這個類的名字和oncreate方法名,已及當前行數(shù); 2
看到上圖中的Log日志是不是很方便定位程序中的問題了,出現(xiàn)異常也能快速的定位到。然后把下面的Log類型在程序的任何地方設置為false則不會打印日志,使用起來相當?shù)姆奖?。?/p>
// 容許打印日志的類型,默認是true,設置為false則不打印 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;
代碼貼在下面:
package com.finddreams.log; 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; import android.os.Environment; import android.text.TextUtils; import android.util.Log; /** * Log工具,類似android.util.Log。 tag自動產(chǎn)生,格式: * customTagPrefix:className.methodName(Line:lineNumber), * customTagPrefix為空時只輸出:className.methodName(Line:lineNumber)。 */ public class LogUtils { public static String customTagPrefix = "finddreams"; // 自定義Tag的前綴,可以是作者名 private static final boolean isSaveLog = false; // 是否把保存日志到SD卡中 public static final String ROOT = Environment.getExternalStorageDirectory() .getPath() + "/finddreams/"; // SD卡中的根目錄 private static final String PATH_LOG_INFO = ROOT + "info/"; private LogUtils() { } // 容許打印日志的類型,默認是true,設置為false則不打印 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; } /** * 自定義的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(PATH_LOG_INFO, 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(PATH_LOG_INFO, 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); } } public static void v(String content) { if (!allowV) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.v(tag, content); } else { Log.v(tag, content); } } public static void v(String content, Throwable tr) { if (!allowV) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.v(tag, content, tr); } else { Log.v(tag, content, tr); } } public static void w(String content) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, content); } else { Log.w(tag, content); } } public static void w(String content, Throwable tr) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, content, tr); } else { Log.w(tag, content, tr); } } public static void w(Throwable tr) { if (!allowW) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.w(tag, tr); } else { Log.w(tag, tr); } } public static void wtf(String content) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, content); } else { Log.wtf(tag, content); } } public static void wtf(String content, Throwable tr) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, content, tr); } else { Log.wtf(tag, content, tr); } } public static void wtf(Throwable tr) { if (!allowWtf) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); if (customLogger != null) { customLogger.wtf(tag, tr); } else { Log.wtf(tag, tr); } } private static StackTraceElement getCallerStackTraceElement() { return Thread.currentThread().getStackTrace()[4]; } public static void point(String path, String tag, String msg) { if (isSDAva()) { Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE); dateFormat.applyPattern("yyyy"); path = path + dateFormat.format(date) + "/"; dateFormat.applyPattern("MM"); path += dateFormat.format(date) + "/"; dateFormat.applyPattern("dd"); path += dateFormat.format(date) + ".log"; dateFormat.applyPattern("[yyyy-MM-dd HH:mm:ss]"); String time = dateFormat.format(date); File file = new File(path); if (!file.exists()) createDipPath(path); BufferedWriter out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file, true))); out.write(time + " " + tag + " " + msg + "\r\n"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } } /** * 根據(jù)文件路徑 遞歸創(chuàng)建文件 * * @param file */ public static void createDipPath(String file) { String parentFile = file.substring(0, file.lastIndexOf("/")); File file1 = new File(file); File parent = new File(parentFile); if (!file1.exists()) { parent.mkdirs(); try { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } /** * A little trick to reuse a formatter in the same thread */ private static class ReusableFormatter { private Formatter formatter; private StringBuilder builder; public ReusableFormatter() { builder = new StringBuilder(); formatter = new Formatter(builder); } public String format(String msg, Object... args) { formatter.format(msg, args); String s = builder.toString(); builder.setLength(0); return s; } } private static final ThreadLocal<ReusableFormatter> thread_local_formatter = new ThreadLocal<ReusableFormatter>() { protected ReusableFormatter initialValue() { return new ReusableFormatter(); } }; public static String format(String msg, Object... args) { ReusableFormatter formatter = thread_local_formatter.get(); return formatter.format(msg, args); } public static boolean isSDAva() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) || Environment.getExternalStorageDirectory().exists()) { return true; } else { return false; } } }
以上這篇android 日志文件LogUtils實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。