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
package com.yanzuoguang.util.log;
import com.yanzuoguang.util.extend.ConfigBase;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.Date;
import java.util.HashMap;
/**
* 日志操作类
*
* @author 颜佐光
*/
public class Log {
/**
* 当前线程缓存的对象
*/
private static final HashMap<String, LogDate> threadCache = new HashMap<>();
/**
* 缓存的日志处理对象
*/
private static final RunnableLog writeLogDefault = new LogDefault();
/**
* 写入错误消息
*
* @param cls 类
* @param msg
* @param args
*/
public static void error(Class<?> cls, String msg, Object... args) {
String toMsg = getFormat(msg, args);
add(new LogInfo(cls, new Date(), true, null, toMsg));
}
/**
* 写入错误信息
*
* @param ex
*/
public static void error(Class<?> cls, Throwable ex) {
add(new LogInfo(cls, new Date(), ex != null, ex, StringHelper.EMPTY));
}
/**
* 写入错误信息
*
* @param ex 错误内容
* @param msg 错误消息
* @param args 错误消息
*/
public static void error(Class<?> cls, Throwable ex, String msg, Object... args) {
String toMsg = getFormat(msg, args);
add(new LogInfo(cls, new Date(), ex != null, ex, toMsg));
}
/**
* 警告信息
*
* @param msg
* @param args
*/
public static void info(Class<?> cls, String msg, Object... args) {
String toMsg = getFormat(msg, args);
add(new LogInfo(cls, new Date(), false, null, toMsg));
}
/**
* 警告信息
*
* @param msg
* @param args
*/
public static void infoTag(Class<?> cls, String tag, String msg, Object... args) {
String toMsg = getFormat(msg, args);
LogInfo info = new LogInfo(cls, new Date(), null, toMsg);
info.setTag(tag);
add(info);
}
/**
* 将日志添加到处理队列
*
* @param info 需要处理的信息
*/
private static void add(LogInfo info) {
// 获取当前线程编号
String threadId = getThreadId();
info.setThreadId(threadId);
// 判断当前线程日志是否需要特殊处理
LogDate date;
synchronized (Log.class) {
if (!threadCache.containsKey(threadId)) {
date = threadBegin();
} else {
date = threadCache.get(threadId);
}
}
if (date == null) {
return;
}
date.commit();
info.setTime((long) date.getLastSecond());
info.setTotalTime((long) date.getTotalSecond());
if (ConfigBase.getWriteLog() != null) {
ConfigBase.getWriteLog().run(info);
} else {
writeLogDefault.run(info);
}
}
/**
* 当前线程特殊处理异常,一旦调用必须调用 threadCommit 函数
*/
public static LogDate threadBegin() {
// 获取当前线程编号
String threadId = getThreadId();
LogDate logDate = new LogDate();
logDate.clear();
threadCache.put(threadId, logDate);
return logDate;
}
/**
* 当前县城日志对象
*
* @return
*/
public static LogDate threadCurrent() {
String threadId = getThreadId();
return threadCache.getOrDefault(threadId, null);
}
/**
* 当前线程结束处理特殊异常
*/
public static LogDate threadCommit() {
String threadId = getThreadId();
LogDate log = threadCurrent();
threadCache.remove(threadId);
return log;
}
/**
* 获取当前线程编号
*
* @return
*/
private static String getThreadId() {
return String.valueOf(Thread.currentThread().getId());
}
/**
* 将异常消息格式化
*
* @param msg
* @param args
* @return
*/
private static String getFormat(String msg, Object... args) {
if (args != null && args.length > 0) {
try {
msg = String.format(msg, args);
} catch (Exception ex) {
Log.error(Log.class, ex);
}
}
return msg;
}
}