Android日志打印类LogUtils,能够定位到类名,方法名以及出现错

  关注finddreams,一起分享,一起进步!      在开发中,我们常常用打印log的方式来调试我们的应用。在Java中我们常常使用方法System.out.println()来在控制台打印日志,以便我们的调试。Android中有一个专门的类Log来实现在Android系统下日志的打印,更加方便我们定位程序出现问题的地方。      但是Android官方提供的Log类在实际项目使用中,也不是非常方便。当程序出现错误时,我们最希望的就是这个Log类能帮我们定位到是哪个类的哪个方法,甚至于是那一行出现了错误。这样就能给我们的调试带来很大的便利。      同时我们也应该想到为了应用程序的安全起见,在app正式上线之前,我们应该要把打印日志的功能关闭掉,以防别人通过Log来破解你的应用。生产模式的下打印Log,正式模式就把打印日志的功能关闭掉,要做到Log的灵活关闭与打开,也需要在原生的Log类上进行一些封装。      还有一种时候,当我们的程序出现问题崩溃了,,我们希望能够收集到出现异常的原因进行分析,所以可以把Log日志保存到一个文件中,放在SD卡程序创建的目录下。也可以在用户联网的情况下,在程序的后台把出异常的Log日志文件上传到服务端,方便程序员进行分析,解决bug。      今天就给大家分享一个做项目中很实用的一个Log类LogUtils,这个类是从xUtils中提取出来,稍作修改的,有注释。      示例:   我们在MainActivity中调用一些LogUtils中的方法,注意看行数。     

 接着看看控制台打印的日志是不是像MainActivity调用的那样,Log中有这个类的名字和oncreate方法名,已及当前行数;  

  看到上图中的Log日志是不是很方便定位程序中的问题了,出现异常也能快速的定位到。然后把下面的Log类型在程序的任何地方设置为false则不会打印日志,使用起来相当的方便。  

 allowD = true;allowE = true;allowI = true;allowV = true;allowW = true;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自动产生,格式: * customTagPrefix:className.methodName(Line:lineNumber), * customTagPrefix为空时只输出:className.methodName(Line:lineNumber)。 * */{isSaveLog = String ROOT = Environment.getExternalStorageDirectory().getPath() + String PATH_LOG_INFO = ROOT + “info/”;private LogUtils() {}allowD = true;allowE = true;allowI = true;allowV = true;allowW = true;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;{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);}(String content) {if (!allowD)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.d(tag, content);} else {Log.d(tag, content);}}(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);}}(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);}}(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());}}(String content) {if (!allowI)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.i(tag, content);} else {Log.i(tag, content);}}(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);}}(String content) {if (!allowV)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.v(tag, content);} else {Log.v(tag, content);}}(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);}}(String content) {if (!allowW)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.w(tag, content);} else {Log.w(tag, content);}}(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);}}(Throwable tr) {if (!allowW)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.w(tag, tr);} else {Log.w(tag, tr);}}(String content) {if (!allowWtf)return;StackTraceElement caller = getCallerStackTraceElement();String tag = generateTag(caller);if (customLogger != null) {customLogger.wtf(tag, content);} else {Log.wtf(tag, content);}}(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);}}(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];}(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) + “http://blog.csdn.net/”;dateFormat.applyPattern(“MM”);path += dateFormat.format(date) + “http://blog.csdn.net/”;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();}}}}/*** 根据文件路径 递归创建文件** @param file*/(String file) {String parentFile = file.substring(0, file.lastIndexOf(“http://blog.csdn.net/”));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 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;}}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);}() {if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)|| Environment.getExternalStorageDirectory().exists()) {return true;} else {return false;}}}

我们一路上兴致勃勃地参观,当夕阳西下时,才恋恋不舍地离开。

Android日志打印类LogUtils,能够定位到类名,方法名以及出现错

相关文章:

你感兴趣的文章:

标签云: