在下面的示例中进行了尝试,但是它不适用于spring.Spring中不支持在编辑器中获得err,例如“调用切入点指定符”。

https://dzone.com/articles/enforcing-common-log-format

任何代码示例将不胜感激。

最佳答案

有很多方法可以做到这一点,这里是1。

1)创建一个注释以添加到需要添加日志的类/方法中:

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecution {
}


2)创建一个方面来进行日志记录:

@Aspect
@Component
public class LogAspect {

    private List<String> messages = new ArrayList<>();

    @Around("@annotation(hello.LogExecution)")
    public Object handelLogging(ProceedingJoinPoint joinPoint) throws Throwable {

        Object proceed = null;
        try {
           // log before
           proceed = joinPoint.proceed();
           // log after
        }
        catch (Exception e) {
           // log exception
           throw e;
        }
        return proceed;
    }
}


工作示例here

08-03 22:29