How to use Java annotation to automatically record logs

In Java, annotations can be used to automatically record logs. The following is a sample code that shows how to use Java annotation to automatically record logs. Firstly, create a custom annotation '@ Log' to mark the method that you want to automatically record logs. import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Log { } Next, create a 'LogAspect' class to define facet logic, which involves logging before and after the method annotated with '@ Log' is executed. import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import java.util.logging.Logger; @Aspect public class LogAspect { private static Logger logger = Logger.getLogger(LogAspect.class.getName()); @Pointcut("@annotation(Log)") public void logPointcut() { } @Before("logPointcut()") public void logBefore(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); logger.info("Before executing method: " + methodName); } @After("logPointcut()") public void logAfter(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); logger.info("After executing method: " + methodName); } } In the above code, the 'LogAspect' class uses AspectJ annotations to declare a facet and defines a pointcut 'logPointcut()' that specifies the annotation '@ Log'. In the 'logBefore()' and 'logAfter()' methods, information about the annotated method, such as the method name, can be obtained through the 'JoinPoint' object. Finally, we create a simple test class' Example 'and use the' @ Log 'annotation to mark the method that we want to automatically record logs. public class Example { @Log public void method1() { System.out.println("Executing method1"); } public void method2() { System.out.println("Executing method2"); } public static void main(String[] args) { Example example = new Example(); example.method1(); example.method2(); } } Running the above code will output the following logs on the console: Before executing method: method1 Executing method1 After executing method: method1 Executing method2 Through the above code, we have achieved the function of automatically recording logs before and after the method annotated with '@ Log' is executed. Summary: 1. Use Java annotation to easily mark the method to automatically record logs. 2. By utilizing AspectJ annotations and facet programming, we can dynamically capture the execution process of annotated methods at runtime. 3. Slice logic can perform logging or other operations before and after the annotated method is executed. 4. Using Java annotation to automatically record logs can improve the maintainability and readability of the code.