文章目录
  1. 1. 概要
  2. 2. 减少一个 Log 参数
  3. 3. 跳转到源代码 Log 调用处
  4. 4. 屏蔽所有 Log
  5. 5. 使用方法

工欲善其事,必先利其器。在 Android 开发过程中,相信都会遇到这样的问题:

  • Log 太多,不容易找到自己设置打印的内容
  • 找到 Log 但是定位到 Java 源码有不是很方便
  • 对 Log 打印需要设置 Tag,大多时候 Tag 就是本类的类名,存在重复的工作
  • 发布正式版本需要注释或者关掉很多 Log 开关

概要

参考 Android 源码,使用官方的 Log 打印方法(设置Tag),然后再 Eclipse 里面设置过滤标签,这样就可以查看某一个类的 Log 了,但是这样效率比较低下。基于上面的一些问题,我研究实现了一种简单易用的 Log 工具类。我的工具类主要有如下特点:

  • 减少一个 Log 参数
  • 调用某些 Log 方法,可以实现跳转到 Java 源代码的功能
  • 采用此工具类,屏蔽 Log 只需要关闭 Log 总开关即可

减少一个 Log 参数

实现方法是采用 StackTraceElement 获取调用类的方法,代码如下:

1
2
3
4
5
6
private static String getClassName() {
String result = "";
StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];
result = thisMethodStack.getClassName();
return result;
}

跳转到源代码 Log 调用处

实现双击 Log 跳转到 Java 源代码调用处,实现方法是参考 Android Logcat 错处日志,发现 Eclipse 实现跳转是因为 Log 中有 at MyClassName.MyMethodName(MClassName.java:行数) 。具体见代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* Realization of double click jump events.
*
* @return
*/

private static String callMethodAndLine() {
String result = "at ";
StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];
result += thisMethodStack.getClassName() + ".";
result += thisMethodStack.getMethodName();
result += "(" + thisMethodStack.getFileName();
result += ":" + thisMethodStack.getLineNumber() + ") ";
return result;
}

屏蔽所有 Log

采用 LogUtils 工具类,只需要将此类中DEBUG_LEVEL设置的值大一点。

  • 如果想要屏蔽所有的 Log 只需要将 DEBUG_LEVEL 设置为 Log.ASSERT 即可。
  • 如果想要只显示 Error 则只需要将 DEBUG_LEVEL 设置为 Log.WARN 即可。
  • 如果只想要显示 Error 和 Warn 则将 DEBUG_LEVEL 设置为 Log.INFO 即可。

使用方法

只需要用 LogUtils 替换掉 Android 系统的 Log 类,并去掉 log 方法的第一个参数,例如:

1
LogUtils.d(Object); // 次行代码将打印Object类的toString()

无代码无真相,下面是此工具类源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* Copyright 2014 Zhenguo Jin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.worthed.util;

import android.util.Log;

/**
* This class can replace android.util.Log.
*
* @author jingle1267@163.com
*
* @description And you can turn off the log by set DEBUG_LEVEL = Log.ASSERT.
*
*/

public class LogUtils {

/**
* Master switch.To catch error info you need set this value below Log.WARN
*/

public static final int DEBUG_LEVEL = 0;

/**
* 'System.out' switch.When it is true, you can see the 'System.out' log.
* Otherwise, you cannot.
*/

public static final boolean DEBUG_SYSOUT = false;

/**
* Send a {@link #VERBOSE} log message.
*
* @param obj
*/

public static void v(Object obj) {
if (Log.VERBOSE > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.v(tag, msg);
}
}

/**
* Send a {@link #DEBUG_LEVEL} log message.
*
* @param obj
*/

public static void d(Object obj) {
if (Log.DEBUG > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.d(tag, msg);
}
}

/**
* Send an {@link #INFO} log message.
*
* @param obj
*/

public static void i(Object obj) {
if (Log.INFO > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.i(tag, msg);
}
}

/**
* Send a {@link #WARN} log message.
*
* @param obj
*/

public static void w(Object obj) {
if (Log.WARN > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.w(tag, msg);
}
}

/**
* Send an {@link #ERROR} log message.
*
* @param obj
*/

public static void e(Object obj) {
if (Log.ERROR > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.e(tag, msg);
}
}

/**
* What a Terrible Failure: Report a condition that should never happen. The
* error will always be logged at level ASSERT with the call stack.
* Depending on system configuration, a report may be added to the
* {@link android.os.DropBoxManager} and/or the process may be terminated
* immediately with an error dialog.
*
* @param obj
*/

public static void wtf(Object obj) {
if (Log.ASSERT > DEBUG_LEVEL) {
String tag = getClassName();
String msg = obj != null ? obj.toString() : "obj == null";
Log.wtf(tag, msg);
}
}

/**
* Send a {@link #VERBOSE} log message.
*
* @param tag
* Used to identify the source of a log message. It usually
* identifies the class or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/

public static void v(String tag, String msg) {
if (Log.VERBOSE > DEBUG_LEVEL) {
Log.v(tag, msg);
}
}

/**
* Send a {@link #DEBUG_LEVEL} log message.
*
* @param tag
* Used to identify the source of a log message. It usually
* identifies the class or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/

public static void d(String tag, String msg) {
if (Log.DEBUG > DEBUG_LEVEL) {
Log.d(tag, msg);
}
}

/**
* Send an {@link #INFO} log message.
*
* @param tag
* Used to identify the source of a log message. It usually
* identifies the class or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/

public static void i(String tag, String msg) {
if (Log.INFO > DEBUG_LEVEL) {
Log.i(tag, msg);
}
}

/**
* Send a {@link #WARN} log message.
*
* @param tag
* Used to identify the source of a log message. It usually
* identifies the class or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/

public static void w(String tag, String msg) {
if (Log.WARN > DEBUG_LEVEL) {
Log.w(tag, msg);
}
}

/**
* Send an {@link #ERROR} log message.
*
* @param tag
* Used to identify the source of a log message. It usually
* identifies the class or activity where the log call occurs.
* @param msg
* The message you would like logged.
*/

public static void e(String tag, String msg) {
if (Log.ERROR > DEBUG_LEVEL) {
Log.e(tag, msg);
}
}

/**
* What a Terrible Failure: Report a condition that should never happen. The
* error will always be logged at level ASSERT with the call stack.
* Depending on system configuration, a report may be added to the
* {@link android.os.DropBoxManager} and/or the process may be terminated
* immediately with an error dialog.
*
* @param tag
* Used to identify the source of a log message.
* @param msg
* The message you would like logged.
*/

public static void wtf(String tag, String msg) {
if (Log.ASSERT > DEBUG_LEVEL) {
Log.wtf(tag, msg);
}
}

/**
* Send a {@link #VERBOSE} log message. And just print method name and
* position in black.
*/

public static void print() {
if (Log.VERBOSE > DEBUG_LEVEL) {
String tag = getClassName();
String method = callMethodAndLine();
Log.v(tag, method);
if (DEBUG_SYSOUT) {
System.out.println(tag + " " + method);
}
}
}

/**
* Send a {@link #DEBUG_LEVEL} log message.
*
* @param object
* The object to print.
*/

public static void print(Object object) {
if (Log.DEBUG > DEBUG_LEVEL) {
String tag = getClassName();
String method = callMethodAndLine();
String content = "";
if (object != null) {
content = object.toString() + " ---- "
+ method;
} else {
content = " ## " + " ---- " + method;
}
Log.d(tag, content);
if (DEBUG_SYSOUT) {
System.out.println(tag + " " + content + " " + method);
}
}
}

/**
* Send an {@link #ERROR} log message.
*
* @param object
* The object to print.
*/

public static void printError(Object object) {
if (Log.ERROR > DEBUG_LEVEL) {
String tag = getClassName();
String method = callMethodAndLine();
String content = "";
if (object != null) {
content = object.toString() + " ---- "
+ method;
} else {
content = " ## " + " ---- " + method;
}
Log.e(tag, content);
if (DEBUG_SYSOUT) {
System.err.println(tag + " " + method + " " + content);
}
}
}

/**
* Print the array of stack trace elements of this method in black.
*
* @return
*/

public static void printCallHierarchy() {
if (Log.VERBOSE > DEBUG_LEVEL) {
String tag = getClassName();
String method = callMethodAndLine();
String hierarchy = getCallHierarchy();
Log.v(tag, method + hierarchy);
if (DEBUG_SYSOUT) {
System.out.println(tag + " " + method + hierarchy);
}
}
}

/**
* Print debug log in blue.
*
* @param object
* The object to print.
*/

public static void printMyLog(Object object) {
if (Log.DEBUG > DEBUG_LEVEL) {
String tag = "MYLOG";
String method = callMethodAndLine();
String content = "";
if (object != null) {
content = object.toString() + " ---- "
+ method;
} else {
content = " ## " + " ---- " + method;
}
Log.d(tag, content);
if (DEBUG_SYSOUT) {
System.out.println(tag + " " + content + " " + method);
}
}
}

private static String getCallHierarchy() {
String result = "";
StackTraceElement[] trace = (new Exception()).getStackTrace();
for (int i = 2; i < trace.length; i++) {
result += "\r\t" + trace[i].getClassName() + "."
+ trace[i].getMethodName() + "():"
+ trace[i].getLineNumber();
}
return result;
}

private static String getClassName() {
String result = "";
StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];
result = thisMethodStack.getClassName();
return result;
}

/**
* Realization of double click jump events.
*
* @return
*/

private static String callMethodAndLine() {
String result = "at ";
StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[2];
result += thisMethodStack.getClassName() + ".";
result += thisMethodStack.getMethodName();
result += "(" + thisMethodStack.getFileName();
result += ":" + thisMethodStack.getLineNumber() + ") ";
return result;
}

}

获取更多请参看我 Github 分享的 Android 工具类库 android-utils


本文地址 http://ihongqiqu.com/2014/10/16/android-log/ 作者为 Zhenguo

author:Zhenguo
Author: Zhenguo      Blog: http://ihongqiqu.com/     Email: jinzhenguo1990@gmail.com
I have almost 6 years of Android application development experience and have a keen interested in the latest emerging technologies. I use my spare time to turn my experience, ideas and love for Android tech into informative articles, tutorials and more in hope to help others and learn more.
文章目录
  1. 1. 概要
  2. 2. 减少一个 Log 参数
  3. 3. 跳转到源代码 Log 调用处
  4. 4. 屏蔽所有 Log
  5. 5. 使用方法
返回顶部