提交 ec716f99 编写于 作者: Z zhengjie

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

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