1.0版本完成
This commit is contained in:
76
yshop-logging/src/main/java/co/yixiang/aspect/LogAspect.java
Normal file
76
yshop-logging/src/main/java/co/yixiang/aspect/LogAspect.java
Normal file
@ -0,0 +1,76 @@
|
||||
package co.yixiang.aspect;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.utils.RequestHolder;
|
||||
import co.yixiang.utils.SecurityUtils;
|
||||
import co.yixiang.utils.StringUtils;
|
||||
import co.yixiang.utils.ThrowableUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import co.yixiang.service.LogService;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
private long currentTime = 0L;
|
||||
|
||||
/**
|
||||
* 配置切入点
|
||||
*/
|
||||
@Pointcut("@annotation(co.yixiang.aop.log.Log)")
|
||||
public void logPointcut() {
|
||||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
*/
|
||||
@Around("logPointcut()")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object result = null;
|
||||
currentTime = System.currentTimeMillis();
|
||||
result = joinPoint.proceed();
|
||||
Log log = new Log("INFO",System.currentTimeMillis() - currentTime);
|
||||
logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()),joinPoint, log);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置异常通知
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
* @param e exception
|
||||
*/
|
||||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
||||
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime);
|
||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
|
||||
logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()), (ProceedingJoinPoint)joinPoint, log);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
try {
|
||||
return SecurityUtils.getUsername();
|
||||
}catch (Exception e){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user