java代码
package com.nusp.Config;
import com.nusp.Entity.Common.JobOperationLog;
import com.nusp.Service.Common.CommonServer;
import com.nusp.Utils.IdUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
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.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;
/**
* Web层日志切面
*/
@Aspect //这里使用@Aspect注解方式设置AOP
@Order(5) //值越小,越先加载
@Component
public class WebLogAspect {
@Autowired
CommonServer commonServer;
private Logger logger = Logger.getLogger(getClass());
ThreadLocal<Long> startTime = new ThreadLocal<>();
//这里@Pointcut设置切点可以设置为Controller层的地址
@Pointcut("execution( * com.nusp.Controller.*.*.*(..))")
public void webLog() {
}
//@Before指在切点方法之前执行,也就是在Controller层方法执行之前执行,这里可以通过JoinPoint获取一些有关方法的信息,在这里也可以修改参数的值
//@Before()括号里设置的是切点方法的名称
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//操作日志对象
JobOperationLog log = new JobOperationLog();
//通过反射获取到类,填入类名
Class cl1 = Class.forName(joinPoint.getSignature().getDeclaringTypeName());
Method[] methods = cl1.getMethods();
// //获取ApiOperation注解
for(Method method:methods)
{
//joinPoint.getSignature().getName()方法名
if(method.getName().equals(joinPoint.getSignature().getName()))
{
ApiOperation anno = method.getAnnotation(ApiOperation.class);
if (anno != null) {
log.setMethodName(anno.value());
}
}}
log.setId(IdUtils.createUuid());
log.setUrl(request.getRequestURL().toString());
log.setIp(request.getRemoteAddr());
log.setHttpMethod(request.getMethod());
log.setUserId(12313);
log.setCreateTime(new Date());
log.setClassMethod(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
commonServer.savelog(log);
}
}
实体
package com.nusp.Entity.Common;
import lombok.Data;
import java.util.Date;
@Data
public class JobOperationLog {
private String id;
private String methodName;
private String url;
private String ip;
private String httpMethod;
private Integer userId;
private Date createTime;
private String classMethod;
}
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
sql
INSERT INTO
job_operation_log
(
id,
url,
http_method,
ip,
class_method,
create_time,
user_id,
method_name
)
VALUES
(
#{id},
#{url},
#{httpMethod},
#{ip},
#{classMethod},
#{createTime},
#{userId},
#{methodName}
)
建表语句
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 100506
Source Host : localhost:3306
Source Schema : archives
Target Server Type : MySQL
Target Server Version : 100506
File Encoding : 65001
Date: 09/09/2022 14:17:32
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for job_operation_log
-- ----------------------------
DROP TABLE IF EXISTS `job_operation_log`;
CREATE TABLE `job_operation_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '请求接口地址',
`http_method` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '请求方式',
`ip` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'ip地址',
`class_method` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'class方法',
`create_time` datetime(0) NULL DEFAULT NULL,
`user_id` int(11) NULL DEFAULT NULL COMMENT '用户id',
`method_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '方法说明',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 97 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '操作日志' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
完结撒花