提交 ec716f99 编写于 作者: Z zhengjie

优化 druid配置,日志异步保存

上级 3696c2fb
...@@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j; ...@@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j;
import me.zhengjie.domain.Log; import me.zhengjie.domain.Log;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.service.LogService; import me.zhengjie.service.LogService;
import me.zhengjie.utils.RequestHolder;
import me.zhengjie.utils.SecurityUtils;
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;
...@@ -14,6 +16,8 @@ import org.aspectj.lang.annotation.Pointcut; ...@@ -14,6 +16,8 @@ import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/** /**
* @author jie * @author jie
* @date 2018-11-24 * @date 2018-11-24
...@@ -47,7 +51,7 @@ public class LogAspect { ...@@ -47,7 +51,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(joinPoint, log); logService.save(getUsername(), RequestHolder.getHttpServletRequest(),joinPoint, log);
return result; return result;
} }
...@@ -61,6 +65,14 @@ public class LogAspect { ...@@ -61,6 +65,14 @@ 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((ProceedingJoinPoint)joinPoint, log); logService.save(getUsername(), RequestHolder.getHttpServletRequest(), (ProceedingJoinPoint)joinPoint, log);
}
public String getUsername() {
try {
return SecurityUtils.getUsername();
}catch (Exception e){
return "";
}
} }
} }
...@@ -4,6 +4,8 @@ import me.zhengjie.domain.Log; ...@@ -4,6 +4,8 @@ import me.zhengjie.domain.Log;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import javax.servlet.http.HttpServletRequest;
/** /**
* @author jie * @author jie
* @date 2018-11-24 * @date 2018-11-24
...@@ -16,7 +18,7 @@ public interface LogService { ...@@ -16,7 +18,7 @@ public interface LogService {
* @param log * @param log
*/ */
@Async @Async
void save(ProceedingJoinPoint joinPoint, Log log); void save(String username, HttpServletRequest request, ProceedingJoinPoint joinPoint, Log log);
/** /**
* 查询异常详情 * 查询异常详情
......
...@@ -5,7 +5,6 @@ import cn.hutool.json.JSONObject; ...@@ -5,7 +5,6 @@ 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.RequestHolder;
import me.zhengjie.utils.SecurityUtils; import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils; import me.zhengjie.utils.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
...@@ -32,10 +31,8 @@ public class LogServiceImpl implements LogService { ...@@ -32,10 +31,8 @@ public class LogServiceImpl implements LogService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void save(ProceedingJoinPoint joinPoint, Log log){ public void save(String username, HttpServletRequest request, ProceedingJoinPoint joinPoint, Log log){
// 获取request
HttpServletRequest request = RequestHolder.getHttpServletRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod(); Method method = signature.getMethod();
me.zhengjie.aop.log.Log aopLog = method.getAnnotation(me.zhengjie.aop.log.Log.class); me.zhengjie.aop.log.Log aopLog = method.getAnnotation(me.zhengjie.aop.log.Log.class);
...@@ -53,9 +50,6 @@ public class LogServiceImpl implements LogService { ...@@ -53,9 +50,6 @@ public class LogServiceImpl implements LogService {
Object[] argValues = joinPoint.getArgs(); Object[] argValues = joinPoint.getArgs();
//参数名称 //参数名称
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames(); String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
// 用户名
String username = "";
if(argValues != null){ if(argValues != null){
for (int i = 0; i < argValues.length; i++) { for (int i = 0; i < argValues.length; i++) {
params += " " + argNames[i] + ": " + argValues[i]; params += " " + argNames[i] + ": " + argValues[i];
...@@ -65,9 +59,7 @@ public class LogServiceImpl implements LogService { ...@@ -65,9 +59,7 @@ public class LogServiceImpl implements LogService {
// 获取IP地址 // 获取IP地址
log.setRequestIp(StringUtils.getIP(request)); log.setRequestIp(StringUtils.getIP(request));
if(!LOGINPATH.equals(signature.getName())){ if(LOGINPATH.equals(signature.getName())){
username = SecurityUtils.getUsername();
} else {
try { try {
JSONObject jsonObject = new JSONObject(argValues[0]); JSONObject jsonObject = new JSONObject(argValues[0]);
username = jsonObject.get("username").toString(); username = jsonObject.get("username").toString();
......
...@@ -23,6 +23,8 @@ spring: ...@@ -23,6 +23,8 @@ spring:
test-while-idle: true test-while-idle: true
test-on-borrow: false test-on-borrow: false
test-on-return: false test-on-return: false
validation-query: select 1
# 配置监控统计拦截的filters # 配置监控统计拦截的filters
filters: stat filters: stat
stat-view-servlet: stat-view-servlet:
......
...@@ -23,6 +23,7 @@ spring: ...@@ -23,6 +23,7 @@ spring:
test-while-idle: true test-while-idle: true
test-on-borrow: false test-on-borrow: false
test-on-return: false test-on-return: false
validation-query: select 1
# 配置监控统计拦截的filters # 配置监控统计拦截的filters
filters: stat filters: stat
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册