@Aspect标签:String,requestURI,args,iname,public,日志,remoteAddr,参出,springboot From: https://www.cnblogs.com/xiaoBastard/p/18373418
@Slf4j
@Component
@Order(-99)
public class LogHandler {
public static Map<String, AtomicInteger> nm = new ConcurrentHashMap();
@Value("${aop.log.no.enable:true}")
private boolean enableNo;
@Pointcut("execution(* com.test.controller.*Controll*.*(..))")
public void pointcut() {
}
@Pointcut("execution(* translateEnergy(..)) || execution(* mixAudioWrite(..))")
public void exclude_pointcut() {
}
@Around("pointcut() && !exclude_pointcut()")
public Object cost(ProceedingJoinPoint pj) throws Throwable {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String requestURI = request.getRequestURI();
String requestMethod = request.getMethod();
String remoteAddr = HttpWebUtil.getIpAddr(request);
int n = 0;
if (enableNo) {
nm.putIfAbsent(remoteAddr, new AtomicInteger(1));
n = nm.get(remoteAddr).getAndIncrement();
}
MethodSignature signature = (MethodSignature) pj.getSignature();
ApiOperation annotation = signature.getMethod().getAnnotation(ApiOperation.class);
String iname = annotation == null ? "接口" : annotation.value();
Object[] args = pj.getArgs();
if (!excludeInputInterfaces(requestURI)) {
if (enableNo) {
log.info("【aop】{}.开始调用:[{}]{}, ip:{}, method:{}, 参数:{}", n, iname, requestURI, remoteAddr, requestMethod, JSONUtil.toJsonStr(args));
} else {
log.info("【aop】开始调用:[{}]{}, ip:{}, method:{}, 参数:{}", iname, requestURI, remoteAddr, requestMethod, JSONUtil.toJsonStr(args));
}
}else {
log.info("【aop】开始调用:[{}]{}, ip:{}, method:{}, 参数:该接口不打印入参", iname, requestURI, remoteAddr, requestMethod);
}
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof String) {
args[i] = ((String) args[i]).trim();
}
}
long bg = System.currentTimeMillis();
Object r = pj.proceed(args);
long ed = System.currentTimeMillis();
if (!excludeOutputInterfaces(requestURI)) {
if (enableNo) {
log.info("【aop】{}.结束调用:[{}]{}, ip:{}, 耗时:{}ms. 返回结果:{}", n, iname, requestURI, remoteAddr, ed - bg, JSONUtil.toJsonStr(r));
} else {
log.info("【aop】结束调用:[{}]{}, ip:{}, 耗时:{}ms. 返回结果:{}", iname, requestURI, remoteAddr, ed - bg, JSONUtil.toJsonStr(r));
}
}else {
log.info("【aop】结束调用:[{}]{}, ip:{}, 耗时:{}ms. 返回结果:该接口不打印出参", iname, requestURI, remoteAddr, ed - bg);
}
if (enableNo) {
if (requestURI.contains("/testWs")) {
nm.clear();
}
}
return r;
} catch (Throwable throwable) {
throw throwable;
}
}
/**
* 排除不打印入参接口
* @param requestURI
* @return
*/
public boolean excludeInputInterfaces(String requestURI){
List<String> interfacesList = Arrays.asList("/abc/util/abc");
return interfacesList.contains(requestURI);
}
/**
* 排除不打印出参接口
* @param requestURI
* @return
*/
public boolean excludeOutputInterfaces(String requestURI){
List<String> interfacesList = Arrays.asList("/abc/util/abc");
return interfacesList.contains(requestURI);
}