阅读本文前,请先学习完成我的上一篇文章。上一篇文章地址
AOP介绍:
略
Logback介绍:
略
一、AOP添加依赖
使用AOP前,需要在pom.xml文件中添加依赖,然后再maven中重载项目拉取依赖包
org.springframework.boot spring-boot-starter-aop2.6.2
二、在aspectj下自定义一个注解,作为日志切入点,并定义两个字段来存服务码和服务名
三、在Controller中的接口方法上面使用自定义的注解,并输入服务名和服务码
package com.jicr.springboottest.aspectj;
import java.lang.annotation.*;
@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@documented
public @interface OperLog {
String serverCode() default ""; // 服务名
String serverDesc() default ""; // 服务中文描述
}
四、建立OperLogAspect类,用来打印日志。一般会使用接口运行前日志方法(使用@Before注解方法),接口成功运行后日志方法(使用@AfterReturning注解方法),接口出现异常日志方法(使用@AfterThrowing注解方法),接口最终执行日志方法(使用@After注解方法)。
package com.jicr.springboottest.aspectj;
import com.jicr.springboottest.utils.JsonUtils;
import net.sf.json.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class OperLogAspect {
@Pointcut("@annotation(com.jicr.springboottest.aspectj.OperLog)")
public void operLogPoinCut() {
}
@Pointcut("execution(* com.jicr.springboottest.controller..*.*(..))")
public void operExceptionLogPoinCut() {
}
@Before("operLogPoinCut()") //在切入点的方法run之前要干的
public void logBeforeController(JoinPoint joinPoint) {
System.out.println("aop开始打印方法run运行前日志:");
try {
// 从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// 获取切入点所在的方法
Method method = signature.getMethod();
// 获取操作
OperLog opLog = method.getAnnotation(OperLog.class);
String serverCode = "";
String serverDesc = "";
if (opLog != null) {
serverCode = opLog.serverCode();
serverDesc = opLog.serverDesc();
System.out.println("服务码:" + serverCode); // 服务码
System.out.println("服务中文描述:" + serverDesc); // 服务中文描述
}
String reqContent = JsonUtils.objectToJson(joinPoint.getArgs());//请求报文转json格式
String reqContentSub = reqContent.substring(1, reqContent.length() - 1);//去掉两边的[]
JSonObject jsonObject = JsonUtils.jsonTojsonObject(reqContentSub);//json字符串转json对象
System.out.println("请求报文:"+jsonObject);
String seq=jsonObject.getJSonObject("head").get("head_seq_no").toString();
String headReqDate=jsonObject.getJSonObject("head").get("head_req_date").toString();
String headReqtime=jsonObject.getJSonObject("head").get("head_req_time").toString();
String headReqOrgan=jsonObject.getJSonObject("head").get("head_snd_organ").toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@AfterReturning(value = "operLogPoinCut()", returning = "returnOb")
public void saveOperLog(JoinPoint joinPoint, Object returnOb) {
System.out.println("aop开始打印正常返回通知:");
try {
JSonObject jsonReturn = JsonUtils.jsonTojsonObject(String.valueOf(returnOb));
String headRspDate = jsonReturn.getJSonObject("head").get("head_rsp_date").toString();
String headRspTime = jsonReturn.getJSonObject("head").get("head_rsp_time").toString();
System.out.println("响应报文:"+jsonReturn);
}catch (Exception e){
System.out.println(e.getMessage());
}
}
//异常返回通知,用于拦截异常日志信息 连接点抛出异常后执行
@AfterThrowing(pointcut = "operExceptionLogPoinCut()", throwing = "e")
public void saveExceptionLog(JoinPoint joinPoint, Throwable e) {
System.out.println("aop开始打印异常日志信息:");
}
//后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
@After("operLogPoinCut()")
public void after(JoinPoint joinPoint) {
System.out.println("aop开始打印后置最终通知");
try {
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}



