Logger.java
2.95 KB
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
package com.sw.laryngoscope.utils;
import android.util.Log;
/**
* 日志相关类:默认是测试环境<br>
* <b>支持:存储Log日志文件到本地。发送Log日志信息到服务器</b>
*
* @author
* @since 2018-6-18 12:38:39
*/
public class Logger {
public static boolean isDebug = true;
private final static String APP_TAG = "D";
/**
* 获取相关数据:类名,方法名,行号等.用来定位行<br>
* at cn.utils.BigMenuActivity.onCreate(BigMenuActivity.java:17) 就是用來定位行的代碼<br>
*
* @return [ Thread:main, at
* cn.utils.BigMenuActivity.onCreate(BigMenuActivity.java:17)]
*/
private static String getFunctionName() {
StackTraceElement[] sts = Thread.currentThread().getStackTrace();
if (sts != null) {
for (StackTraceElement st : sts) {
if (st.isNativeMethod()) {
continue;
}
if (st.getClassName().equals(Thread.class.getName())) {
continue;
}
if (st.getClassName().equals(Logger.class.getName())) {
continue;
}
/*return "[ Thread:" + Thread.currentThread().getName() + ", at " + st.getClassName() + "." + st.getMethodName()
+ "(" + st.getFileName() + ":" + st.getLineNumber() + ")" + " ]";*/
return "[ (" + st.getFileName() + ":" + st.getLineNumber() + ")" + " ]";
}
}
return null;
}
public static void v(String msg) {
if (isDebug) {
Log.v(APP_TAG, getMsgFormat(msg));
}
}
public static void v(String tag, String msg) {
if (isDebug) {
Log.v(tag, getMsgFormat(msg));
}
}
public static void d(String msg) {
if (isDebug) {
Log.d(APP_TAG, getMsgFormat(msg));
}
}
public static void d(String tag, String msg) {
if (isDebug) {
Log.d(tag, getMsgFormat(msg));
}
}
public static void i(String msg) {
if (isDebug) {
Log.i(APP_TAG, getMsgFormat(msg));
}
}
public static void i(String tag, String msg) {
if (isDebug) {
Log.i(tag, getMsgFormat(msg));
}
}
public static void w(String msg) {
if (isDebug) {
Log.w(APP_TAG, getMsgFormat(msg));
}
}
public static void w(String tag, String msg) {
if (isDebug) {
Log.w(tag, getMsgFormat(msg));
}
}
public static void e(String msg) {
if (isDebug) {
Log.e(APP_TAG, getMsgFormat(msg));
}
}
public static void e(String tag, String msg) {
if (isDebug) {
Log.e(tag, getMsgFormat(msg));
}
}
/**
* 输出格式定义
*/
private static String getMsgFormat(String msg) {
return msg + " ;" + getFunctionName();
}
}