Android輸出log的封裝

來源:互聯網
上載者:User

標籤:

本文中的log封裝來源於github一個項目中,具體位置我給忘了.經過此封裝後可以方便查看到log所在的類名,方法名,行數等資訊.‘

 1 public class DLog { 2     static String className; 3     static String methodName; 4     static int lineNumber; 5       6     private DLog(){ 7         /* Protect from instantiations */ 8     } 9  10     public static boolean isDebuggable() {11         return BuildConfig.DEBUG;12     }13  14     private static String createLog( String log ) {15          16         StringBuffer buffer = new StringBuffer();17         buffer.append("[");18         buffer.append(methodName);19         buffer.append(":");20         buffer.append(lineNumber);21         buffer.append("]");22         buffer.append(log);23  24         return buffer.toString();25     }26      27     private static void getMethodNames(StackTraceElement[] sElements){28         className = sElements[1].getFileName();29         methodName = sElements[1].getMethodName();30         lineNumber = sElements[1].getLineNumber();31     }32  33     public static void e(String message){34         if (!isDebuggable()){35             return;36         }37  38         getMethodNames(new Throwable().getStackTrace());39         Log.e(className, createLog(message));40     }41  42     public static void i(String message){43         if (!isDebuggable()){44             return;45         }46  47         getMethodNames(new Throwable().getStackTrace());48         Log.i(className, createLog(message));49     }50      51     public static void d(String message){52         if (!isDebuggable()){53             return;54         }55  56         getMethodNames(new Throwable().getStackTrace());57         Log.d(className, createLog(message));58     }59      60     public static void v(String message){61         if (!isDebuggable()){62             return;63         }64          65         getMethodNames(new Throwable().getStackTrace());66         Log.v(className, createLog(message));67     }68      69     public static void w(String message){70         if (!isDebuggable()){71             return;72         }73  74         getMethodNames(new Throwable().getStackTrace());75         Log.w(className, createLog(message));76     }77      78     public static void wtf(String message){79         if (!isDebuggable()){80             return;81         }82  83         getMethodNames(new Throwable().getStackTrace());84         Log.wtf(className, createLog(message));85     }86 }

 

Android輸出log的封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.