mx-community/src/main/java/cn/allms/aspect/LogAspectOld.java
2021-07-04 03:39:13 +08:00

99 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.allms.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
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.Arrays;
/**
* @author josxy
*/
@Component
public class LogAspectOld {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 切点cn.allms.controller
*/
//@Pointcut("execution(* cn.allms.controller.*.*(..))")
public void log() {
}
// @Before("log()")
public void doBefore(JoinPoint joinPoint) {
// 获取request对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
// URL
String url = request.getRequestURL().toString();
// IP
String ip = request.getRemoteAddr();
// 方法全名 = 类名.方法名
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
// 请求对象
Object[] args = joinPoint.getArgs();
RequestLog requestLog = new RequestLog(url, ip, classMethod, args);
logger.info("Request : {}", requestLog);
}
//@After("log()")
public void doAfter() {
// logger.info("-------doafter-----------");
}
public void doAfterReturn(Object result) {
logger.info("控制器方法名称Result : {}", result);
}
/**
* 内部类:用于存放请求信息,用日志的方式将其记录下来
*/
private class RequestLog {
/**
* 请求地址
*/
private String url;
/**
* 请求ip
*/
private String ip;
/**
* 请求方法
*/
private String classMethod;
/**
* 请求参数集合
*/
private Object[] args;
public RequestLog(String url, String ip, String classMethod, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = classMethod;
this.args = args;
}
@Override
public String toString() {
return "RequestLog{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}