package com.zhx.guides.assistant.web.aspect;
import com.alibaba.fastjson.JSONObject;
import com.zhx.guides.assistant.dto.HttpRequestLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @Class WebLogAspect
* @Version 1.0
* @Date 创建时间:2020-03-03 10:05
* @Copyright Copyright by company
* @Direction 类说明
*/
@Aspect
@Component
public class WebLogAspect {
@Autowired
private MongoTemplate mongoTemplate;
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
/** 以 controller 包下定义的所有请求为切入点 */
@Pointcut(“execution(public * com.zhx.guides.assistant.interfaces.controller..*.*(..))”)
public void webLog() {}
/**
* 在切点之前织入
* @param joinPoint
* @throws Throwable
*/
@Before(“webLog()”)
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 开始打印请求日志
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
HttpRequestLog httpRequestLog = new HttpRequestLog() ;
httpRequestLog.setUrl( request.getRequestURL().toString() );
httpRequestLog.setHttpMethod( request.getMethod() );
httpRequestLog.setClassName( joinPoint.getSignature().getDeclaringTypeName() );
httpRequestLog.setMethodName( joinPoint.getSignature().getName());
httpRequestLog.setIp( request.getRemoteAddr() );
// 打印请求相关参数
logger.info(“======================= Start ======================”);
// 打印请求 url
logger.info(“URL : {}”, httpRequestLog.getUrl() );
// 打印 Http method
logger.info(“HTTP Method : {}”, httpRequestLog.getHttpMethod() );
// 打印调用 controller 的全路径以及执行方法
logger.info(“Class Method : {}.{}”, httpRequestLog.getClassName() , httpRequestLog.getMethodName());
// 打印请求的 IP
logger.info(“IP : {}”, httpRequestLog.getIp() );
// 打印请求入参
try {
Object requestParam = joinPoint.getArgs();
httpRequestLog.setRequestParam( JSONObject.toJSONString(requestParam) );
logger.info(“参数 : {}”, httpRequestLog.getRequestParam() );
}catch (Exception e){
logger.info(“参数打印失败,异常: {}”, e.getMessage() );
}finally {
httpRequestLog.setClassName( null );
try{mongoTemplate.save( httpRequestLog , HttpRequestLog.collectName );}catch (Exception e){}
}
}
/**
* 在切点之后织入
* @throws Throwable
*/
@After(“webLog()”)
public void doAfter() throws Throwable {
logger.info(“======================= End ======================”);
}
/**
* 环绕
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around(“webLog()”)
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
// 打印出参
logger.info(“返回值 : {}”, JSONObject.toJSONString(result));
// 执行耗时
logger.info(“耗时 : {} ms”, System.currentTimeMillis() – startTime);
return result;
}
}