Android封裝對(duì)原生Log進(jìn)行封裝的操作
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
package com.zjx.taobaounion.utils; import android.util.Log; public class LogUtils { private static int currentLev = 4; // 當(dāng)前l(fā)og等級(jí) 上線之后控制這個(gè)等級(jí) 就可以減少Log的輸出 private static final int DEBUG_LEV = 4; // debug 等級(jí) private static final int INFO_LEV = 3; // info 等級(jí) private static final int WARNING_LEV = 2; // warning 等級(jí) private static final int ERROR_LEV = 1; // error 等級(jí) public static void d(Class clazz,String log){ if (currentLev >= DEBUG_LEV) { // 如果當(dāng)前設(shè)置的等級(jí) 大于等于 debug等級(jí) 那么就證明當(dāng)前不屏蔽debug的log輸出 Log.d(clazz.getSimpleName(),log); } } public static void i(Class clazz,String log){ if (currentLev >= INFO_LEV) { // 如果當(dāng)前設(shè)置的等級(jí) 大于等于 info 那么就證明當(dāng)前不屏蔽info的log輸出 Log.i(clazz.getSimpleName(),log); } } public static void w(Class clazz,String log){ if (currentLev >= WARNING_LEV) { // 如果當(dāng)前設(shè)置的等級(jí) 大于等于 warning 那么就證明當(dāng)前不屏蔽warning的log輸出 Log.w(clazz.getSimpleName(),log); } } public static void e(Class clazz,String log){ if (currentLev >= ERROR_LEV) { // 如果當(dāng)前設(shè)置的等級(jí) 大于等于 error 那么就證明當(dāng)前不屏蔽error的log輸出 Log.e(clazz.getSimpleName(),log); } } }
補(bǔ)充知識(shí):android 日志文件LogUtils
背景
這是好久之前在網(wǎng)上找的一個(gè)常用類,已經(jīng)忘記原文鏈接了,但是覺得很好用一直都在用,可以將日志寫到file里面也可以定位你是在哪個(gè)類哪一行打印的日志,保存到文件的路徑就是android/data/你的包名/files/目錄下,然后我們就可以愉快的找問(wèn)題了
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() { } // 容許打印日志的類型,默認(rèn)是true,設(shè)置為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; } /*** * 打印控制臺(tái)顯示不了那么長(zhǎng)的日志問(wèn)題 * * @param msg */ public static void logE(String msg) { // 信息太長(zhǎng),分段打印 if (!allowE) return; StackTraceElement caller = getCallerStackTraceElement(); String tag = generateTag(caller); // 因?yàn)镾tring的length是字符數(shù)量不是字節(jié)數(shù)量所以為了防止中文字符過(guò)多, // 把4*1024的MAX字節(jié)打印長(zhǎng)度改為2001字符數(shù) int max_str_length = 2001 - tag.length(); // 大于4000時(shí) 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封裝對(duì)原生Log進(jìn)行封裝的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Retrofit的簡(jiǎn)單介紹和使用
這篇文章主要介紹了Android Retrofit的簡(jiǎn)單介紹和使用2017-03-03自定義toast外形,多次點(diǎn)擊不會(huì)總是彈出toast的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇自定義toast外形,多次點(diǎn)擊不會(huì)總是彈出toast的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Flutter 快速實(shí)現(xiàn)聊天會(huì)話列表效果示例詳解
這篇文章主要為大家介紹了Flutter 快速實(shí)現(xiàn)聊天會(huì)話列表效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android Studio導(dǎo)入項(xiàng)目非常慢的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Studio導(dǎo)入項(xiàng)目非常慢的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤
這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤,總結(jié)分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下2016-01-01清楚詳解Android?進(jìn)程間圖傳遞圖形buffer原理
這篇文章主要為大家清楚的詳解了Android?進(jìn)程間圖傳遞圖形buffer原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02