通用日志记录
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.macro.mall.component;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 统一日志处理切面
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class WebLogAspect {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);
|
||||
private ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||
|
||||
@Pointcut("execution(public * com.macro.mall.controller.*.*(..))")
|
||||
public void webLog() {
|
||||
}
|
||||
|
||||
@Before("webLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
//获取当前请求对象
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
//记录请求信息(可以记录到数据库中)
|
||||
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
|
||||
String args = Arrays.toString(joinPoint.getArgs());
|
||||
LOGGER.info("HTTP URL:{}; HTTP_METHOD:{}; IP:{}; CLASS_METHOD:{}; ARGS:{}",
|
||||
request.getRequestURL(), request.getMethod(), request.getRemoteAddr(), classMethod, args);
|
||||
}
|
||||
|
||||
@AfterReturning(value = "webLog()", returning = "ret")
|
||||
public void doAfterReturning(Object ret) throws Throwable {
|
||||
long costTime = System.currentTimeMillis() - startTime.get();
|
||||
LOGGER.info("RESPONSE:{}; COST_TIME:{}ms", ret, costTime);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user