java aop 日誌打印 正則設置
阿新 • • 發佈:2019-04-30
multipart nature 定義 file reel star eof request actor
package tz.lion.Utils.aop;
import com.alibaba.fastjson.JSON;
import org.springframework.web.multipart.MultipartFile;
import tz.lion.config.shiro.SessionHolder;
import tz.lion.domain.master.SysUser;
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.Enumeration;
/**
* aop實現web層的日誌切面
* @author 104
*/
@Aspect
@Component
public class WebLogAspect {
private Logger logger = LoggerFactory.getLogger("webLogAspect");
ThreadLocal<Long> startTime = new ThreadLocal<Long>();
/*** 定義一個切入點
*/
@Pointcut("execution(public * tz.lion.controller.*..*.*(..))")
public void webLog(){}
/**
* 定義一個非切入點
* controller 方法名稱,以UnAop 開頭 不打印日誌;例如文件參數等長字符
*/
@Pointcut("!execution(public * tz.lion.controller.*..*.UnAop*(..))")
private void logSender(){}
/**
* 處理日誌,打印請求相關數據
* @param joinPoint
*/
@Before("webLog()&&logSender()")
public void doBefore(JoinPoint joinPoint){
startTime.set(System.currentTimeMillis());
SysUser user = SessionHolder.getHolder().getSessionAttribute(SessionHolder.CURRENT_USER);
if(user != null){
logger.info("UserId : "+user.getId());
}
// 接收到請求,記錄請求內容
logger.info("WebLogAspect.doBefore()");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
try{
// 記錄下請求內容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
boolean hasFile=false;
for(Object obj:joinPoint.getArgs()){
if(obj instanceof MultipartFile){
hasFile=true;
}
}
if(hasFile){
logger.info("ARGS : 上傳文件接口 ");
}else{
logger.info("ARGS : " + JSON.toJSONString(joinPoint.getArgs()));
}
}catch(Exception e){
logger.info("獲取請求參數失敗");
}
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=enu.nextElement();
logger.info("request paraName ARGS"+paraName+": "+request.getParameter(paraName));
}
}
/**
* 處理完請求,返回內容
* @param joinPoint
*/
@AfterReturning(returning="result", pointcut = "webLog()")
public void doAfterReturning(JoinPoint joinPoint,Object result){
logger.info("WebLogAspect.doAfterReturning()");
String res = JSON.toJSONString(result);
if(res.length()<=2000){
logger.info("返回數據:"+res);
}
logger.info("耗時(毫秒) : " + (System.currentTimeMillis() - startTime.get()));
}
}
java aop 日誌打印 正則設置