Android 日志打印工具类 可显示打印所在的方法和行号

来源:互联网 发布:怎么找网络水军公司 编辑:程序博客网 时间:2024/05/01 12:31

Android 日志打印工具类 可显示打印所在的方法和行号。便于开发人员查看Log信息~~

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.newland.util;  
  2.   
  3. import java.io.BufferedWriter;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStreamWriter;  
  8. import java.text.DateFormat;  
  9. import java.text.SimpleDateFormat;  
  10. import java.util.Date;  
  11. import java.util.Formatter;  
  12. import java.util.Locale;  
  13.   
  14. import android.os.Environment;  
  15. import android.text.TextUtils;  
  16. import android.util.Log;  
  17.   
  18. /** 
  19.  * Log工具,类似android.util.Log。 tag自动产生,格式: 
  20.  * customTagPrefix:className.methodName(Line:lineNumber), 
  21.  * customTagPrefix为空时只输出:className.methodName(Line:lineNumber)。 
  22.  */  
  23. public class LogUtil {  
  24.   
  25.     public static String customTagPrefix = "";  // 自定义Tag的前缀,可以是作者名  
  26.     public static boolean isSaveLog = false;    // 是否把保存日志到SD卡中  
  27.     public static final String LOG_PATH = Environment.getExternalStorageDirectory().getPath(); // SD卡中的根目录  
  28.       
  29.     private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);  
  30.   
  31.     private LogUtil() {  
  32.     }  
  33.   
  34.     // 容许打印日志的类型,默认是true,设置为false则不打印  
  35.     public static boolean allowD = true;  
  36.     public static boolean allowE = true;  
  37.     public static boolean allowI = true;  
  38.     public static boolean allowV = true;  
  39.     public static boolean allowW = true;  
  40.     public static boolean allowWtf = true;  
  41.   
  42.     private static String generateTag(StackTraceElement caller) {  
  43.         String tag = "%s.%s(Line:%d)"// 占位符  
  44.         String callerClazzName = caller.getClassName(); // 获取到类名  
  45.         callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);  
  46.         tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber()); // 替换  
  47.         tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;  
  48.           
  49.         return tag;  
  50.     }  
  51.   
  52.     /** 
  53.      * 自定义的logger 
  54.      */  
  55.     public static CustomLogger customLogger;  
  56.   
  57.     public interface CustomLogger {  
  58.         void d(String tag, String content);  
  59.         void d(String tag, String content, Throwable e);  
  60.           
  61.         void e(String tag, String content);  
  62.         void e(String tag, String content, Throwable e);  
  63.           
  64.         void i(String tag, String content);  
  65.         void i(String tag, String content, Throwable e);  
  66.           
  67.         void v(String tag, String content);  
  68.         void v(String tag, String content, Throwable e);  
  69.           
  70.         void w(String tag, String content);  
  71.         void w(String tag, String content, Throwable e);  
  72.         void w(String tag, Throwable tr);  
  73.           
  74.         void wtf(String tag, String content);  
  75.         void wtf(String tag, String content, Throwable e);  
  76.         void wtf(String tag, Throwable tr);  
  77.     }  
  78.   
  79.     public static void d(String content) {  
  80.         if (!allowD) {  
  81.             return;  
  82.         }  
  83.           
  84.         StackTraceElement caller = getCallerStackTraceElement();  
  85.         String tag = generateTag(caller);  
  86.   
  87.         if (customLogger != null) {  
  88.             customLogger.d(tag, content);  
  89.         } else {  
  90.             Log.d(tag, content);  
  91.         }  
  92.     }  
  93.   
  94.     public static void d(String content, Throwable e) {  
  95.         if (!allowD) {  
  96.             return;  
  97.         }  
  98.           
  99.         StackTraceElement caller = getCallerStackTraceElement();  
  100.         String tag = generateTag(caller);  
  101.   
  102.         if (customLogger != null) {  
  103.             customLogger.d(tag, content, e);  
  104.         } else {  
  105.             Log.d(tag, content, e);  
  106.         }  
  107.     }  
  108.   
  109.     public static void e(String content) {  
  110.         if (!allowE) {  
  111.             return;  
  112.         }  
  113.           
  114.         StackTraceElement caller = getCallerStackTraceElement();  
  115.         String tag = generateTag(caller);  
  116.   
  117.         if (customLogger != null) {  
  118.             customLogger.e(tag, content);  
  119.         } else {  
  120.             Log.e(tag, content);  
  121.         }  
  122.         if (isSaveLog) {  
  123.             point(LOG_PATH, tag, content);  
  124.         }  
  125.     }  
  126.   
  127.     public static void e(Throwable e) {  
  128.         if (!allowE) {  
  129.             return;  
  130.         }  
  131.           
  132.         StackTraceElement caller = getCallerStackTraceElement();  
  133.         String tag = generateTag(caller);  
  134.           
  135.         if (customLogger != null) {  
  136.             customLogger.e(tag, "error", e);  
  137.         } else {  
  138.             Log.e(tag, e.getMessage(), e);  
  139.         }  
  140.         if (isSaveLog) {  
  141.             point(LOG_PATH, tag, e.getMessage());  
  142.         }  
  143.     }  
  144.       
  145.     public static void e(String content, Throwable e) {  
  146.         if (!allowE) {  
  147.             return;  
  148.         }  
  149.           
  150.         StackTraceElement caller = getCallerStackTraceElement();  
  151.         String tag = generateTag(caller);  
  152.   
  153.         if (customLogger != null) {  
  154.             customLogger.e(tag, content, e);  
  155.         } else {  
  156.             Log.e(tag, content, e);  
  157.         }  
  158.         if (isSaveLog) {  
  159.             point(LOG_PATH, tag, e.getMessage());  
  160.         }  
  161.     }  
  162.   
  163.     public static void i(String content) {  
  164.         if (!allowI) {  
  165.             return;  
  166.         }  
  167.           
  168.         StackTraceElement caller = getCallerStackTraceElement();  
  169.         String tag = generateTag(caller);  
  170.   
  171.         if (customLogger != null) {  
  172.             customLogger.i(tag, content);  
  173.         } else {  
  174.             Log.i(tag, content);  
  175.         }  
  176.         if (isSaveLog) {  
  177.             point(LOG_PATH, tag, content);  
  178.         }  
  179.     }  
  180.   
  181.     public static void i(String content, Throwable e) {  
  182.         if (!allowI) {  
  183.             return;  
  184.         }  
  185.           
  186.         StackTraceElement caller = getCallerStackTraceElement();  
  187.         String tag = generateTag(caller);  
  188.   
  189.         if (customLogger != null) {  
  190.             customLogger.i(tag, content, e);  
  191.         } else {  
  192.             Log.i(tag, content, e);  
  193.         }  
  194.         if (isSaveLog) {  
  195.             point(LOG_PATH, tag, content);  
  196.         }  
  197.     }  
  198.   
  199.     public static void v(String content) {  
  200.         if (!allowV) {  
  201.             return;  
  202.         }  
  203.           
  204.         StackTraceElement caller = getCallerStackTraceElement();  
  205.         String tag = generateTag(caller);  
  206.   
  207.         if (customLogger != null) {  
  208.             customLogger.v(tag, content);  
  209.         } else {  
  210.             Log.v(tag, content);  
  211.         }  
  212.         if (isSaveLog) {  
  213.             point(LOG_PATH, tag, content);  
  214.         }  
  215.     }  
  216.   
  217.     public static void v(String content, Throwable e) {  
  218.         if (!allowV) {  
  219.             return;  
  220.         }  
  221.           
  222.         StackTraceElement caller = getCallerStackTraceElement();  
  223.         String tag = generateTag(caller);  
  224.   
  225.         if (customLogger != null) {  
  226.             customLogger.v(tag, content, e);  
  227.         } else {  
  228.             Log.v(tag, content, e);  
  229.         }  
  230.         if (isSaveLog) {  
  231.             point(LOG_PATH, tag, content);  
  232.         }  
  233.     }  
  234.   
  235.     public static void w(String content) {  
  236.         if (!allowW) {  
  237.             return;  
  238.         }  
  239.           
  240.         StackTraceElement caller = getCallerStackTraceElement();  
  241.         String tag = generateTag(caller);  
  242.   
  243.         if (customLogger != null) {  
  244.             customLogger.w(tag, content);  
  245.         } else {  
  246.             Log.w(tag, content);  
  247.         }  
  248.         if (isSaveLog) {  
  249.             point(LOG_PATH, tag, content);  
  250.         }  
  251.     }  
  252.   
  253.     public static void w(String content, Throwable e) {  
  254.         if (!allowW) {  
  255.             return;  
  256.         }  
  257.           
  258.         StackTraceElement caller = getCallerStackTraceElement();  
  259.         String tag = generateTag(caller);  
  260.   
  261.         if (customLogger != null) {  
  262.             customLogger.w(tag, content, e);  
  263.         } else {  
  264.             Log.w(tag, content, e);  
  265.         }  
  266.         if (isSaveLog) {  
  267.             point(LOG_PATH, tag, content);  
  268.         }  
  269.     }  
  270.   
  271.     public static void w(Throwable e) {  
  272.         if (!allowW) {  
  273.             return;  
  274.         }  
  275.           
  276.         StackTraceElement caller = getCallerStackTraceElement();  
  277.         String tag = generateTag(caller);  
  278.   
  279.         if (customLogger != null) {  
  280.             customLogger.w(tag, e);  
  281.         } else {  
  282.             Log.w(tag, e);  
  283.         }  
  284.         if (isSaveLog) {  
  285.             point(LOG_PATH, tag, e.toString());  
  286.         }  
  287.     }  
  288.   
  289.     public static void wtf(String content) {  
  290.         if (!allowWtf) {  
  291.             return;  
  292.         }  
  293.           
  294.         StackTraceElement caller = getCallerStackTraceElement();  
  295.         String tag = generateTag(caller);  
  296.   
  297.         if (customLogger != null) {  
  298.             customLogger.wtf(tag, content);  
  299.         } else {  
  300.             Log.wtf(tag, content);  
  301.         }  
  302.         if (isSaveLog) {  
  303.             point(LOG_PATH, tag, content);  
  304.         }  
  305.     }  
  306.   
  307.     public static void wtf(String content, Throwable e) {  
  308.         if (!allowWtf) {  
  309.             return;  
  310.         }  
  311.           
  312.         StackTraceElement caller = getCallerStackTraceElement();  
  313.         String tag = generateTag(caller);  
  314.   
  315.         if (customLogger != null) {  
  316.             customLogger.wtf(tag, content, e);  
  317.         } else {  
  318.             Log.wtf(tag, content, e);  
  319.         }  
  320.         if (isSaveLog) {  
  321.             point(LOG_PATH, tag, content);  
  322.         }  
  323.     }  
  324.   
  325.     public static void wtf(Throwable e) {  
  326.         if (!allowWtf) {  
  327.             return;  
  328.         }  
  329.           
  330.         StackTraceElement caller = getCallerStackTraceElement();  
  331.         String tag = generateTag(caller);  
  332.   
  333.         if (customLogger != null) {  
  334.             customLogger.wtf(tag, e);  
  335.         } else {  
  336.             Log.wtf(tag, e);  
  337.         }  
  338.         if (isSaveLog) {  
  339.             point(LOG_PATH, tag, e.toString());  
  340.         }  
  341.     }  
  342.   
  343.     private static StackTraceElement getCallerStackTraceElement() {  
  344.         return Thread.currentThread().getStackTrace()[4];  
  345.     }  
  346.   
  347.     public static void point(String path, String tag, String msg) {  
  348.         if (isSDAva()) {  
  349.             long timestamp = System.currentTimeMillis();  
  350.             String time = formatter.format(new Date());  
  351.             path = path + "/logs/log-" + time + "-" + timestamp + ".log";  
  352.               
  353.             File file = new File(path);  
  354.             if (!file.exists()) {  
  355.                 createDipPath(path);  
  356.             }  
  357.             BufferedWriter out = null;  
  358.             try {  
  359.                 out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));  
  360.                 out.write(time + " " + tag + " " + msg + "\r\n");  
  361.             } catch (Exception e) {  
  362.                 e.printStackTrace();  
  363.             } finally {  
  364.                 try {  
  365.                     if (out != null) {  
  366.                         out.close();  
  367.                     }  
  368.                 } catch (IOException e) {  
  369.                     e.printStackTrace();  
  370.                 }  
  371.             }  
  372.         }  
  373.     }  
  374.   
  375.     /** 
  376.      * 根据文件路径 递归创建文件 
  377.      */  
  378.     public static void createDipPath(String file) {  
  379.         String parentFile = file.substring(0, file.lastIndexOf("/"));  
  380.         File file1 = new File(file);  
  381.         File parent = new File(parentFile);  
  382.         if (!file1.exists()) {  
  383.             parent.mkdirs();  
  384.             try {  
  385.                 file1.createNewFile();  
  386.             } catch (IOException e) {  
  387.                 e.printStackTrace();  
  388.             }  
  389.         }  
  390.     }  
  391.   
  392.     private static class ReusableFormatter {  
  393.   
  394.         private Formatter formatter;  
  395.         private StringBuilder builder;  
  396.   
  397.         public ReusableFormatter() {  
  398.             builder = new StringBuilder();  
  399.             formatter = new Formatter(builder);  
  400.         }  
  401.   
  402.         public String format(String msg, Object... args) {  
  403.             formatter.format(msg, args);  
  404.             String s = builder.toString();  
  405.             builder.setLength(0);  
  406.             return s;  
  407.         }  
  408.     }  
  409.   
  410.     private static final ThreadLocal<ReusableFormatter> thread_local_formatter = new ThreadLocal<ReusableFormatter>() {  
  411.         protected ReusableFormatter initialValue() {  
  412.             return new ReusableFormatter();  
  413.         }  
  414.     };  
  415.   
  416.     public static String format(String msg, Object... args) {  
  417.         ReusableFormatter formatter = thread_local_formatter.get();  
  418.         return formatter.format(msg, args);  
  419.     }  
  420.   
  421.     private static boolean isSDAva() {  
  422.         return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || Environment.getExternalStorageDirectory().exists();  
  423.     }  
  424. }  
0 0
原创粉丝点击