保存日志改为异步

master
zhangw 2018-05-12 22:29:14 +08:00
parent 36b1a4956e
commit 846aeba6b2
2 changed files with 18 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.boot.security.server.advice; package com.boot.security.server.advice;
import com.boot.security.server.utils.UserUtil;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
@ -16,7 +17,7 @@ import io.swagger.annotations.ApiOperation;
/** /**
* *
* *
* @author * @author
* *
* 2017819 * 2017819
@ -31,6 +32,7 @@ public class LogAdvice {
@Around(value = "@annotation(com.boot.security.server.annotation.LogAnnotation)") @Around(value = "@annotation(com.boot.security.server.annotation.LogAnnotation)")
public Object logSave(ProceedingJoinPoint joinPoint) throws Throwable { public Object logSave(ProceedingJoinPoint joinPoint) throws Throwable {
SysLogs sysLogs = new SysLogs(); SysLogs sysLogs = new SysLogs();
sysLogs.setUser(UserUtil.getLoginUser()); // 设置当前登录用户
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
String module = null; String module = null;
@ -50,17 +52,18 @@ public class LogAdvice {
try { try {
Object object = joinPoint.proceed(); Object object = joinPoint.proceed();
sysLogs.setFlag(true); sysLogs.setFlag(true);
logService.save(sysLogs);
return object; return object;
} catch (Exception e) { } catch (Exception e) {
sysLogs.setFlag(false); sysLogs.setFlag(false);
sysLogs.setRemark(e.getMessage()); sysLogs.setRemark(e.getMessage());
logService.save(sysLogs);
throw e; throw e;
} } finally {
if (sysLogs.getUser() != null) {
logService.save(sysLogs);
}
}
} }
} }

View File

@ -24,14 +24,21 @@ public class SysLogServiceImpl implements SysLogService {
@Autowired @Autowired
private SysLogsDao sysLogsDao; private SysLogsDao sysLogsDao;
/**
* 2018.05.12,
*
* @param sysLogs
* @see com.boot.security.server.advice.LogAdvice
*/
@Async
@Override @Override
public void save(SysLogs sysLogs) { public void save(SysLogs sysLogs) {
SysUser user = UserUtil.getLoginUser(); // SysUser user = UserUtil.getLoginUser();
if (user == null || user.getId() == null) { if (sysLogs == null || sysLogs.getUser() == null || sysLogs.getUser().getId() == null) {
return; return;
} }
sysLogs.setUser(user); // sysLogs.setUser(user);
sysLogsDao.save(sysLogs); sysLogsDao.save(sysLogs);
} }