|
@ -1,15 +1,77 @@ |
|
|
package com.canvas.web.modules.logging.aspect; |
|
|
package com.canvas.web.modules.logging.aspect; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.canvas.web.modules.logging.domain.Log; |
|
|
|
|
|
import com.canvas.web.modules.logging.service.LogService; |
|
|
|
|
|
import com.canvas.web.utils.RequestHolder; |
|
|
|
|
|
import com.canvas.web.utils.SecurityUtils; |
|
|
|
|
|
import com.canvas.web.utils.StringUtils; |
|
|
|
|
|
import com.canvas.web.utils.ThrowableUtil; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
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.Aspect; |
|
|
|
|
|
import org.aspectj.lang.annotation.Pointcut; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
@Aspect |
|
|
@Aspect |
|
|
@Slf4j |
|
|
@Slf4j |
|
|
public class LogAspect { |
|
|
public class LogAspect { |
|
|
|
|
|
|
|
|
|
|
|
private final LogService logService; |
|
|
|
|
|
|
|
|
|
|
|
ThreadLocal<Long> currentTime = new ThreadLocal<>(); |
|
|
|
|
|
|
|
|
|
|
|
public LogAspect(LogService logService){ |
|
|
|
|
|
this.logService=logService; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//切入点 |
|
|
|
|
|
@Pointcut("@annotation(com.canvas.web.modules.logging.annotation.Log)") |
|
|
|
|
|
public void logPointcut(){ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置环绕通知 |
|
|
|
|
|
@Around("logPointcut()") |
|
|
|
|
|
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{ |
|
|
|
|
|
Object result; |
|
|
|
|
|
currentTime.set(System.currentTimeMillis()); |
|
|
|
|
|
result=joinPoint.proceed(); |
|
|
|
|
|
Log log=new Log("INFO",System.currentTimeMillis() - currentTime.get()); |
|
|
|
|
|
currentTime.remove(); |
|
|
|
|
|
HttpServletRequest request= RequestHolder.getHttpServletRequest(); |
|
|
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request),StringUtils.getIp(request),joinPoint,log); |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//配置异常通知 |
|
|
|
|
|
@AfterThrowing(pointcut = "logPointcut()", throwing = "e") |
|
|
|
|
|
public void logAfterThrowing(JoinPoint joinPoint, Throwable e){ |
|
|
|
|
|
Log log=new Log("ERROR",System.currentTimeMillis() - currentTime.get()); |
|
|
|
|
|
currentTime.remove(); |
|
|
|
|
|
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes()); |
|
|
|
|
|
HttpServletRequest request = RequestHolder.getHttpServletRequest(); |
|
|
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, log); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getUsername(){ |
|
|
|
|
|
try{ |
|
|
|
|
|
return SecurityUtils.getCurrentUsername(); |
|
|
|
|
|
}catch (Exception e){ |
|
|
|
|
|
return ""; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |