自用AOP切面日志代码,放在aspect目录下即可,日后只需要修改包名
@Aspect
@Component
@Slf4j
@SuppressWarnings("all")
public class WebLogAspect {
@Autowired
private ObjectMapper mapper;
private static final Integer PRINY_LOG_SIZE_LIMIT = 100;
@Pointcut("execution(public * com.yase.system.controller..*.*(..)))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable{
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.debug("========================== Request Coming =========================");
log.debug("URL:[{}]",request.getRequestURI().toString());
log.debug("HTTP Method:[{}]",request.getMethod());
log.debug("Class Method:[{}].[{}]",joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName());
String requestArgs = mapper.writeValueAsString(joinPoint.getArgs());
if(requestArgs.length() >PRINY_LOG_SIZE_LIMIT ){
log.trace("Request Args : [{}]",requestArgs);
}else {
log.debug("Request Args : [{}]",requestArgs);
}
}
@After("webLog()")
public void doAfter() throws Throwable{
log.debug("========================== Request Done =========================");
log.debug("");
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
long oldTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
String resultjson = mapper.writeValueAsString(result);
if(resultjson.length() >PRINY_LOG_SIZE_LIMIT ){
log.trace("Request Args : [{}]",resultjson);
}else {
log.debug("Request Args : [{}]",resultjson);
}
long time = System.currentTimeMillis();
log.debug("Time Elapsed : [{}]",time - oldTime);
return result;
}
}