Skip to content

Commit

Permalink
优化 druid配置,日志异步保存
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjie committed Jun 3, 2019
1 parent 3696c2f commit ec716f9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
16 changes: 14 additions & 2 deletions eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand All @@ -14,6 +16,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
* @author jie
* @date 2018-11-24
Expand Down Expand Up @@ -47,7 +51,7 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
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;
}

Expand All @@ -61,6 +65,14 @@ 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((ProceedingJoinPoint)joinPoint, log);
logService.save(getUsername(), RequestHolder.getHttpServletRequest(), (ProceedingJoinPoint)joinPoint, log);
}

public String getUsername() {
try {
return SecurityUtils.getUsername();
}catch (Exception e){
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.scheduling.annotation.Async;

import javax.servlet.http.HttpServletRequest;

/**
* @author jie
* @date 2018-11-24
Expand All @@ -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);

/**
* 查询异常详情
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand All @@ -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);
Expand All @@ -53,9 +50,6 @@ public void save(ProceedingJoinPoint joinPoint, Log log){
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];
Expand All @@ -65,9 +59,7 @@ public void save(ProceedingJoinPoint joinPoint, Log log){
// 获取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();
Expand Down
2 changes: 2 additions & 0 deletions eladmin-system/src/main/resources/config/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ spring:
test-while-idle: true
test-on-borrow: false
test-on-return: false
validation-query: select 1
# 配置监控统计拦截的filters
filters: stat

Expand Down

0 comments on commit ec716f9

Please sign in to comment.