看到許多不優(yōu)雅的方法,唯有這個(gè)看著最好看
@Before("log()")
public void beforeLog(JoinPoint point) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
log.info("【請(qǐng)求 URL】:{}", request.getRequestURL());
log.info("【請(qǐng)求 IP】:{}", request.getRemoteAddr());
log.info("【請(qǐng)求類名】:{},【請(qǐng)求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
log.info("【body】:{},", JSONUtil.toJsonStr(point.getArgs()));
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("【請(qǐng)求參數(shù)】:{},", JSONUtil.toJsonStr(parameterMap));
Long start = System.currentTimeMillis();
request.setAttribute(START_TIME, start);
}
就是這一句point.getArgs()其實(shí)aspectj已經(jīng)替我們作好這一切了
package com.xkcoding.log.aop.aspectj;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.hutool.json.JSONUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
/**
* @author www.gaozz.club
* @功能描述 aop 中獲取requestbody參數(shù)
* @date 2018-08-26
*/
@Aspect
@Component
@Slf4j
public class AopLog {
private static final String START_TIME = "request-start";
/**
* 切入點(diǎn)
*/
@Pointcut("execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))")
public void log() {
}
/**
* 前置操作
*
* @param point 切入點(diǎn)
*/
@Before("log()")
public void beforeLog(JoinPoint point) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
log.info("【請(qǐng)求 URL】:{}", request.getRequestURL());
log.info("【請(qǐng)求 IP】:{}", request.getRemoteAddr());
log.info("【請(qǐng)求類名】:{},【請(qǐng)求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
log.info("【body】:{},", JSONUtil.toJsonStr(point.getArgs()));
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("【請(qǐng)求參數(shù)】:{},", JSONUtil.toJsonStr(parameterMap));
Long start = System.currentTimeMillis();
request.setAttribute(START_TIME, start);
}
/**
* 環(huán)繞操作
*
* @param point 切入點(diǎn)
* @return 原方法返回值
* @throws Throwable 異常信息
*/
@Around("log()")
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
Object result = point.proceed();
log.info("【返回值】:{}", JSONUtil.toJsonStr(result));
return result;
}
/**
* 后置操作
*/
@AfterReturning("log()")
public void afterReturning() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
Long start = (Long) request.getAttribute(START_TIME);
Long end = System.currentTimeMillis();
log.info("【請(qǐng)求耗時(shí)】:{}毫秒", end - start);
String header = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(header);
log.info("【瀏覽器類型】:{},【操作系統(tǒng)】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header);
}
}