mirror of
https://github.com/liuweijw/fw-cloud-framework.git
synced 2026-03-13 08:50:21 +08:00
异步日志优化
This commit is contained in:
@@ -13,8 +13,6 @@ import com.github.liuweijw.core.beans.system.AuthLog;
|
||||
import com.github.liuweijw.core.beans.system.Log;
|
||||
import com.github.liuweijw.core.commons.constants.CommonConstant;
|
||||
import com.github.liuweijw.core.commons.constants.MqQueueConstant;
|
||||
import com.github.liuweijw.core.commons.jwt.JwtUtil;
|
||||
import com.github.liuweijw.core.configuration.JwtConfiguration;
|
||||
|
||||
/**
|
||||
* 日志队列消息监听:消息对象必须是经过序列化操作的对象
|
||||
@@ -28,15 +26,10 @@ public class LogRabbitListener {
|
||||
@Autowired
|
||||
private LogInfoService logInfoService;
|
||||
|
||||
@Autowired
|
||||
private JwtConfiguration jwtConfiguration;
|
||||
|
||||
@RabbitHandler
|
||||
public void receive(AuthLog authLog) {
|
||||
String username = JwtUtil.getUserName(authLog.getToken(), jwtConfiguration.getJwtkey());
|
||||
MDC.put(CommonConstant.KEY_USER, username);
|
||||
Log sysLog = authLog.getLog();
|
||||
authLog.getLog().setCreateBy(username);
|
||||
MDC.put(CommonConstant.KEY_USER, authLog.getLog().getCreateBy());
|
||||
LogInfo logInfo = new LogInfo();
|
||||
BeanUtils.copyProperties(sysLog, logInfo);
|
||||
logInfoService.saveOrUpdate(logInfo);
|
||||
|
||||
@@ -8,8 +8,6 @@ public class AuthLog implements Serializable {
|
||||
|
||||
private Log log;
|
||||
|
||||
private String token;
|
||||
|
||||
public Log getLog() {
|
||||
return log;
|
||||
}
|
||||
@@ -18,12 +16,4 @@ public class AuthLog implements Serializable {
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.springframework.amqp.core.AmqpTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.github.liuweijw.core.beans.system.AuthLog;
|
||||
@@ -83,12 +85,12 @@ public class LogServiceImpl implements LogService {
|
||||
syslog.setException(throwable.getMessage());
|
||||
}
|
||||
|
||||
// 保存发往MQ(只保存授权请求)
|
||||
AuthLog authLog = new AuthLog();
|
||||
authLog.setLog(syslog);
|
||||
String headAuthorization = request.getHeader(CommonConstant.REQ_HEADER);
|
||||
if (StringHelper.isNotEmpty(headAuthorization)) {
|
||||
authLog.setToken(headAuthorization);
|
||||
// 保存发往MQ(只保存授权)
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && StringHelper.isNotBlank(authentication.getName())) {
|
||||
syslog.setCreateBy(authentication.getName());
|
||||
authLog.setLog(syslog);
|
||||
rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, authLog);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user