5 changed files with 105 additions and 0 deletions
-
14common/src/main/java/com/canvas/web/utils/RequestHolder.java
-
4common/src/main/java/com/canvas/web/utils/SpringSUtiles.java
-
8common/src/main/java/com/canvas/web/utils/SpringUtils.java
-
17common/src/main/java/com/canvas/web/utils/ThrowableUtil.java
-
62system/src/main/java/com/canvas/web/modules/logging/aspect/LogAspect.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(); |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
package com.canvas.web.utils; |
|||
|
|||
public class SpringSUtiles extends org.apache.commons.lang3.StringUtils{ |
|||
} |
@ -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(); |
|||
} |
|||
} |
|||
} |
@ -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 ""; |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue