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

pull/88/head
zhengjie 2019-06-03 09:28:45 +08:00
parent 3696c2fbdd
commit ec716f99f2
5 changed files with 22 additions and 13 deletions

View File

@ -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 "";
}
}
}

View File

@ -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);
/**
*

View File

@ -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();

View File

@ -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:

View File

@ -23,6 +23,7 @@ spring:
test-while-idle: true
test-on-borrow: false
test-on-return: false
validation-query: select 1
# 配置监控统计拦截的filters
filters: stat