Using AOP to Record System Logs:
1.Custom Annotation Class
Define a custom annotation class:
package com.java.common.annotion;
import java.lang.annotation.*;
@Target({ElementType.METHOD, ElementType.PARAMETER}) // This annotation applies to methods and parameters
@Retention(RetentionPolicy.RUNTIME) // Runtime annotation
public @interface SystemControllerLog {
String description() default "Default Controller Interface";
}
2. Define an Aspect Class
In the aspect class, define pointcuts for classes or methods with the custom annotation from step one:
package com.java.common.aspect;
import com.java.common.annotion.SystemControllerLog;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* Aspect class to record common logs
*/
@Component
@Aspect
public class LogAspect {
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
// Pointcut for Controllers
@Pointcut("@annotation(com.java.common.annotion.SystemControllerLog)")
public void controllerAspect() {
}
// Pointcut for Services
@Pointcut("@annotation(com.java.common.annotion.SystemServiceLog)")
public void serviceAspect() {
}
/**
* Before advice to log user actions in Controller layer
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
try {
logger.info("============== Before Advice Start ==============");
logger.info("Request Method: " + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName()));
logger.info("Method Description: " + getControllerMethodDescription(joinPoint));
} catch (Exception exception) {
logger.error("== Before Advice Exception ==");
logger.error("Exception Message: {}", exception.getMessage());
}
}
/**
* Get method description from annotation in Controller layer
*/
private String getControllerMethodDescription(JoinPoint joinPoint) throws ClassNotFoundException {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(SystemControllerLog.class).description();
break;
}
}
}
return description;
}
}
3. Implement the Logging Logic in the Aspect Class
4. Add Annotations to the Classes or Methods that Require Logging
@GetMapping("/user")
@SystemControllerLog(description = "Test API Log")
@ResponseBody
public User user() {
User user = new User();
user.setName("theonefx");
user.setAge(6666);
return user;
}
Assume that the logging code and the business code are in different packages. You need to scan the corresponding aspect class; otherwise, it will not achieve the logging effect.
标签:Use,java,logs,joinPoint,record,org,import,annotation,description From: https://www.cnblogs.com/zhangcheng1234/p/18197471