异步日志

pull/88/head
zhengjie 2019-06-03 13:07:46 +08:00
parent ec716f99f2
commit 2d19ad4915
3 changed files with 6 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.LogService; import me.zhengjie.service.LogService;
import me.zhengjie.utils.RequestHolder; import me.zhengjie.utils.RequestHolder;
import me.zhengjie.utils.SecurityUtils; import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ThrowableUtil; import me.zhengjie.utils.ThrowableUtil;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@ -51,7 +52,7 @@ public class LogAspect {
currentTime = System.currentTimeMillis(); currentTime = System.currentTimeMillis();
result = joinPoint.proceed(); result = joinPoint.proceed();
Log log = new Log("INFO",System.currentTimeMillis() - currentTime); Log log = new Log("INFO",System.currentTimeMillis() - currentTime);
logService.save(getUsername(), RequestHolder.getHttpServletRequest(),joinPoint, log); logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()),joinPoint, log);
return result; return result;
} }
@ -65,7 +66,7 @@ public class LogAspect {
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime); Log log = new Log("ERROR",System.currentTimeMillis() - currentTime);
log.setExceptionDetail(ThrowableUtil.getStackTrace(e)); log.setExceptionDetail(ThrowableUtil.getStackTrace(e));
logService.save(getUsername(), RequestHolder.getHttpServletRequest(), (ProceedingJoinPoint)joinPoint, log); logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()), (ProceedingJoinPoint)joinPoint, log);
} }
public String getUsername() { public String getUsername() {

View File

@ -18,7 +18,7 @@ public interface LogService {
* @param log * @param log
*/ */
@Async @Async
void save(String username, HttpServletRequest request, ProceedingJoinPoint joinPoint, Log log); void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log);
/** /**
* *

View File

@ -5,15 +5,12 @@ import cn.hutool.json.JSONObject;
import me.zhengjie.domain.Log; import me.zhengjie.domain.Log;
import me.zhengjie.repository.LogRepository; import me.zhengjie.repository.LogRepository;
import me.zhengjie.service.LogService; import me.zhengjie.service.LogService;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/** /**
@ -31,7 +28,7 @@ public class LogServiceImpl implements LogService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void save(String username, HttpServletRequest request, ProceedingJoinPoint joinPoint, Log log){ public void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log){
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod(); Method method = signature.getMethod();
@ -57,7 +54,7 @@ public class LogServiceImpl implements LogService {
} }
// 获取IP地址 // 获取IP地址
log.setRequestIp(StringUtils.getIP(request)); log.setRequestIp(ip);
if(LOGINPATH.equals(signature.getName())){ if(LOGINPATH.equals(signature.getName())){
try { try {