Skip to content

Commit

Permalink
异步日志
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjie committed Jun 3, 2019
1 parent ec716f9 commit 2d19ad4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.zhengjie.service.LogService;
import me.zhengjie.utils.RequestHolder;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ThrowableUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
Expand Down Expand Up @@ -51,7 +52,7 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
currentTime = System.currentTimeMillis();
result = joinPoint.proceed();
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;
}

Expand All @@ -65,7 +66,7 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime);
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface LogService {
* @param log
*/
@Async
void save(String username, HttpServletRequest request, ProceedingJoinPoint joinPoint, Log log);
void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log);

/**
* 查询异常详情
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import me.zhengjie.domain.Log;
import me.zhengjie.repository.LogRepository;
import me.zhengjie.service.LogService;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
Expand All @@ -31,7 +28,7 @@ public class LogServiceImpl implements LogService {

@Override
@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();
Method method = signature.getMethod();
Expand All @@ -57,7 +54,7 @@ public void save(String username, HttpServletRequest request, ProceedingJoinPoin
}

// 获取IP地址
log.setRequestIp(StringUtils.getIP(request));
log.setRequestIp(ip);

if(LOGINPATH.equals(signature.getName())){
try {
Expand Down

0 comments on commit 2d19ad4

Please sign in to comment.