天天看點

Android LOG标簽自定義

Android Debugger的LOG标簽(列印資訊不完整),你可以使用StackTraceElement在你的應用裡面實作簡單的LOG列印。你不需要使用System.out這個方法在程式中使用,你可以自己寫個LOG工具類來完成自己的列印需求。

下面是簡單的LogUtils類,提供了一些靜态方法,供大家參考。

1. /**
2.  * @author wangli Log工具類
3.  * 
4.  */  
5. public final class LogUtils {  
6.   
7. private static boolean sIsLogEnabled = true;// 是否打開LOG  
8.   
9. private static String sApplicationTag = "LogDemo";// LOG預設TAG  
10.   
11. private static final String TAG_CONTENT_PRINT = "%s:%s.%s:%d";  
12.   
13. private static StackTraceElement getCurrentStackTraceElement() {  
14. return Thread.currentThread().getStackTrace()[4];  
15.   
16.     }  
17.   
18. //列印LOG  
19. public static void trace() {  
20. if (sIsLogEnabled) {  
21.             android.util.Log.d(sApplicationTag,  
22.                     getContent(getCurrentStackTraceElement()));  
23.         }  
24.     }  
25.   
26. //擷取LOG  
27. private static String getContent(StackTraceElement trace) {  
28. return String.format(TAG_CONTENT_PRINT, sApplicationTag,  
29.                 trace.getClassName(), trace.getMethodName(),  
30.                 trace.getLineNumber());  
31.     }  
32. //列印預設TAG的LOG  
33. public static void traceStack() {  
34. if (sIsLogEnabled) {  
35.             traceStack(sApplicationTag, android.util.Log.ERROR);  
36.         }  
37.     }  
38.   
39. // 列印Log目前調用棧資訊  
40. public static void traceStack(String tag, int priority) {  
41.   
42. if (sIsLogEnabled) {  
43.             StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();  
44.             android.util.Log.println(priority, tag, stackTrace[4].toString());  
45. new StringBuilder();  
46.             String prevClass = null;  
47. for (int i = 5; i < stackTrace.length; i++) {  
48.                 String className = stackTrace[i].getFileName();  
49. int idx = className.indexOf(".java");  
50. if (idx >= 0) {  
51.                     className = className.substring(0, idx);  
52.                 }  
53. if (prevClass == null || !prevClass.equals(className)) {  
54.   
55.                     str.append(className.substring(0, idx));  
56.   
57.                 }  
58.                 prevClass = className;  
59. ".").append(stackTrace[i].getMethodName())  
60. ":").append(stackTrace[i].getLineNumber())  
61. "->");  
62.             }  
63.             android.util.Log.println(priority, tag, str.toString());  
64.         }  
65.     }  
66. //指定TAG和指定内容的方法  
67. public static void d(String tag, String msg) {  
68. if (sIsLogEnabled) {  
69. ">"+msg);  
70.         }  
71.     }  
72. //預設TAG和制定内容的方法  
73. public static void d(String msg) {  
74. if (sIsLogEnabled) {  
75. ">"+msg);  
76.         }  
77.     }  
78. //下面的定義和上面方法相同,可以定義不同等級的Debugger  
79. public static void i(String tag,String msg){  
80.           
81.     }  
82. public static void w(String tag,String msg){  
83.           
84.     }  
85. public static void e(String tag,String msg){  
86.           
87.     }  
88. }      

Activity簡答的按鈕監聽事件。

1. Button mybutton = (Button) findViewById(R.id.mybutton);  
2. mybutton.setOnClickListener(new OnClickListener() {  
3.   
4. @Override  
5. public void onClick(View v) {  
6.     LogUtils.trace();  
7. "test", "在這裡...");  
8.     }  
9. });      

列印結果

1. 09-10 11:48:56.198: D/test(729): LogDemo:com.test.TestScreenActivity$1.onClick:35>在這裡...  
2. 09-10 11:48:56.238: D/test(729): LogDemo:com.test.TestScreenActivity$1.onClick:35>在這裡...      
1. 09-10 11:48:56.198: D/LogDemo(729): LogDemo:com.test.TestScreenActivity$1.onClick:34  
2. 09-10 11:48:56.230: D/LogDemo(729): LogDemo:com.test.TestScreenActivity$1.onClick:34