springboot+aop⾃定义⽇志--收集操作⽇志
背景:
spring boot + aop,⾃定义注解收集controller层⽇志;
1. ⾃定义注解
1.1 ⾃定义注解 l
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId&le.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
1.2 ⾃定义操作⽇志注解
import*;
/**
* ⾃定义操作⽇志注解
*/
@Target({ElementType.PARAMETER,ElementType.METHOD})//注解放置的⽬标位置,METHOD是可注解在⽅法级别上@Retention(RetentionPolicy.RUNTIME)//注解在哪个阶段执⾏
@Documented
public@interface OperLog {
String operModul()default"";//操作模块
String operType()default"";// 操作类型
String operDesc()default"";// 操作说明
}
1.3 ⽤于打印输出⽇志的切⾯
import JSONObject;
import ResultVO;
import Slf4j;
import JoinPoint;
import JoinPoint;
springboot aop
import*;
import MethodSignature;
import Logger;
import LoggerFactory;
import Component;
import RequestContextHolder;
import ServletRequestAttributes;
import HttpServletRequest;
import Objects;
/**
* 切⾯处理类,操作⽇志异常⽇志记录处理
*/
@Aspect
@Component
@Slf4j
public class OperLogAspect {
private Logger logger = Logger(OperLogAspect.class);
/**
* ..表⽰包及⼦包该⽅法代表controller层的所有⽅法  TODO 路径需要根据⾃⼰项⽬定义
*/
@Pointcut("@annotation(com.dexin.pinyougoumon.log.OperLog)")
public void controllerMethod(){
}
/**
* ⽅法执⾏前
*
* @param joinPoint
* @throws Exception
*/
@Before(value ="controllerMethod()")
public void LogRequestInfo(JoinPoint joinPoint)throws Throwable {
logger.info("=================================");
HttpServletRequest request =((ServletRequestAttributes) RequestAttributes())).getRequest();
StringBuilder requestLog =new StringBuilder();
//⽅法名称
String methodName = Signature().getName();
// ⽅法参数
Object[] args = Args();
MethodSignature signature =(MethodSignature) Signature();
OperLog operlog = Method().getAnnotation(OperLog.class);
requestLog.append("请求信息:").append("URL = {").RequestURI()).append("},\n")
.append("请求⽅式 = {").Method()).append("},\n")
.append("请求IP = {").RemoteAddr()).append("},\n")
.append("类⽅法 = {").DeclaringTypeName()).append("}.\n")
.Name()).append("},\t").append("}.\n")
.append("操作类型:{").append(operlog.operType()).append("}.\n")
.append(",操作模块:{").append(operlog.operModul()).append("}.\n")
.append(",操作信息{").append(operlog.operDesc());
// 处理请求参数
String[] paramNames =((MethodSignature) signature).getParameterNames();
Object[] paramValues = Args();
int paramLength =null== paramNames ?0: paramNames.length;
if(paramLength ==0){
requestLog.append("请求参数 = {} ");
}else{
requestLog.append("请求参数 = [");
for(int i =0; i < paramLength -1; i++){
requestLog.append(paramNames[i]).append("=").JSONString(paramValues[i])).append(",");
}
requestLog.append(paramNames[paramLength -1]).append("=").JSONString(paramValues[paramLength -1])).append("]");
}
logger.String());
logger.info("=================================");
}
/**
* ⽅法执⾏后
*
* @param resultVO
* @throws Exception
*/
@AfterReturning(returning ="resultVO", pointcut ="controllerMethod()")
public void logResultVOInfo(ResultVO resultVO)throws Exception {
logger.info("请求结果:"+ Code()+"\t"+ Msg());
logger.info("============end==================");
}
}
2. 将以上java⽂件打成jar,在另外⼀个⼯程引⼊;
3. 在另外⼀个⼯程,创建controller
@RestController
@RequestMapping("/brand")
public class BrandController {
/**
* 返回全部列表
*
* @return
*/
@OperLog(operModul ="BrandController",operType ="findAll",operDesc ="返回全部列表")
@GetMapping("/findAll")
public String findAll(){
return"get all success";
}
}
4. 创建启动类
import MapperScan;
import SpringApplication;
import SpringBootApplication;
import DataSourceAutoConfiguration;
import ComponentScan;
@MapperScan("com.dexin.sellergoods.dao")
@SpringBootApplication(exclude ={DataSourceAutoConfiguration.class})
@ComponentScan(value ="com.dexin.*")
public class SellerGoodsApplication {
public static void main(String[] args){
SpringApplication.run(SellerGoodsApplication.class, args);
}
}
6. 接⼝调⽤⽇志信息如下
7. 注意在启动类上加⼊扫描@ComponentScan 的作⽤就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。