forked from ioniocn/ioniocn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogUtil.java
More file actions
122 lines (105 loc) · 2.87 KB
/
LogUtil.java
File metadata and controls
122 lines (105 loc) · 2.87 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
121
122
//package util.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @Description: 日志工具类
* @Author ioniocn
* @Version: 0.0.1
* @CreateAt 2017年4月14日-上午9:53:46
*
*/
public class LogUtil {
/**
* 获取最原始被调用的堆栈信息
*
* @return
*/
public static StackTraceElement findCaller() {
// 获取堆栈信息
StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
if (null == callStack){
return null;
}
// 最原始被调用的堆栈信息
StackTraceElement caller = null;
// 日志类名称
String logClassName = LogUtil.class.getName();
// 循环遍历到日志类标识
boolean isEachLogClass = false;
// 遍历堆栈信息,获取出最原始被调用的方法信息
for (StackTraceElement s : callStack) {
// 遍历到日志类
if (logClassName.equals(s.getClassName())) {
isEachLogClass = true;
}
// 下一个非日志类的堆栈,就是最原始被调用的方法
if (isEachLogClass) {
if (!logClassName.equals(s.getClassName())) {
isEachLogClass = false;
caller = s;
break;
}
}
}
return caller;
}
/**
* 自动匹配请求类名,生成logger对象,此处 logger name 值为 [className].[methodName]() Line:
* [fileLine]
*
* @return
*/
private static Logger logger() {
// 最原始被调用的堆栈对象
StackTraceElement caller = findCaller();
if (null == caller) {
return LoggerFactory.getLogger(LogUtil.class);
}
Logger log = LoggerFactory
.getLogger(caller.getClassName() + "." + caller.getMethodName() + "() " + caller.getLineNumber());
return log;
}
public static void trace(String msg) {
trace(msg, null);
}
public static void trace(String msg, Throwable e) {
logger().trace(msg, e);
}
public static void debug(String msg) {
debug(msg, null);
}
public static void debug(String msg, Throwable e) {
logger().debug(msg, e);
}
public static void info(String msg) {
info(msg, null);
}
public static void info(String msg, Throwable e) {
logger().info(msg, e);
}
public static void warn(String msg) {
warn(msg, null);
}
public static void warn(String msg, Throwable e) {
logger().warn(msg, e);
}
public static void error(String msg) {
error(msg, null);
}
public static void error(String msg, Throwable e) {
logger().error(msg, e);
}
public static String exMsg(Exception ex) {
StackTraceElement[] eles = ex.getStackTrace();
if (null != eles && eles.length > 0) {
final String msg = ex.getMessage();
StringBuilder builder = new StringBuilder((null == msg ? 0 : msg.length()) + 256);
builder.append(ex.getClass().getName()).append(" ");
builder.append(eles[0].getClassName()).append(".").append(eles[0].getMethodName()).append("()").append(" ")
.append(eles[0].getLineNumber()).append(" ").append(msg);
return builder.toString();
}
return "";
}
}