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
package com.yanzuoguang.cloud.aop.log;
import com.yanzuoguang.cloud.CloudConfig;
import com.yanzuoguang.cloud.aop.LogFeign;
import com.yanzuoguang.util.thread.ThreadNext;
import com.yanzuoguang.util.vo.LogVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 获取当前日志对象
*
* @author 颜佐光
*/
@Component
public class LogBase implements ThreadNext.Next, InitializingBean, ApplicationContextAware {
/**
* 缓存队列
*/
protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<>();
@Autowired
private CloudConfig cloudConfig;
private LogFeignBase logFeign;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 获取个人实例话的对象
try {
this.logFeign = applicationContext.getBean(LogFeign.class);
} catch (Exception ex) {
System.err.println("请添加处理日志服务,实现LogFeign接口!");
}
// 默认实例话对象
if (this.logFeign == null) {
this.logFeign = applicationContext.getBean(LogFeignBase.class);
}
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
// 判断是否要记录日志
if (cloudConfig.isLogBase()) {
ThreadNext.start(this, "save log error");
}
}
/**
* 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响
*
* @param logVo
*/
public void addLog(LogVo logVo) {
// 当不记录日志时,则直接忽略
if (!cloudConfig.isLogBase()) {
return;
}
cache.add(logVo);
}
/**
* 执行下一个函数,出现异常会继续执行
*
* @return 是否继续执行
* @throws Exception 异常信息
*/
@Override
public boolean next() {
if (cache.isEmpty()) {
return true;
}
// 循环处理日志信息
while (cache.size() > 0) {
// 取出一条日志
LogVo item = cache.poll();
// 判断日志信息是否为空
if (item == null) {
continue;
}
// 保存日志
try {
logFeign.save(item);
} catch (Exception ex) {
ex.printStackTrace();
}
}
return true;
}
/**
* 沉睡时间
*
* @return
*/
@Override
public int getNextTime() {
return 100;
}
}