Browse Source

fix loggging

master
刘力 3 years ago
parent
commit
f371fda348
  1. 14
      common/src/main/java/com/canvas/web/utils/RequestHolder.java
  2. 4
      common/src/main/java/com/canvas/web/utils/SpringSUtiles.java
  3. 8
      common/src/main/java/com/canvas/web/utils/SpringUtils.java
  4. 17
      common/src/main/java/com/canvas/web/utils/ThrowableUtil.java
  5. 62
      system/src/main/java/com/canvas/web/modules/logging/aspect/LogAspect.java

14
common/src/main/java/com/canvas/web/utils/RequestHolder.java

@ -0,0 +1,14 @@
package com.canvas.web.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
public class RequestHolder {
public static HttpServletRequest getHttpServletRequest(){
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
}
}

4
common/src/main/java/com/canvas/web/utils/SpringSUtiles.java

@ -0,0 +1,4 @@
package com.canvas.web.utils;
public class SpringSUtiles extends org.apache.commons.lang3.StringUtils{
}

8
common/src/main/java/com/canvas/web/utils/SpringUtils.java

@ -1,5 +1,7 @@
package com.canvas.web.utils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@ -7,6 +9,8 @@ import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class SpringUtils implements BeanFactoryPostProcessor {
@ -53,4 +57,8 @@ public class SpringUtils implements BeanFactoryPostProcessor {
public static <T> T getAopProxy(T invoker) {
return (T) AopContext.currentProxy();
}
}

17
common/src/main/java/com/canvas/web/utils/ThrowableUtil.java

@ -0,0 +1,17 @@
package com.canvas.web.utils;
import java.io.PrintWriter;
import java.io.StringWriter;
public class ThrowableUtil {
//获取堆栈信息
public static String getStackTrace(Throwable throwable){
StringWriter sw= new StringWriter();
try(PrintWriter pw =new PrintWriter(sw)){
throwable.printStackTrace(pw);
return sw.toString();
}
}
}

62
system/src/main/java/com/canvas/web/modules/logging/aspect/LogAspect.java

@ -1,15 +1,77 @@
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 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.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
@Aspect
@Slf4j
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 "";
}
}
}
Loading…
Cancel
Save