AOP 日志记录:@Aspect 未在控制台中记录 log4j 默认配置的错误

AOP Logging: @Aspect is not logging the error in console for log4j default configuration

我是 Spring 的新手,正在尝试使用 log4j 实现 Spring AOP 以在控制台中记录错误。 请注意,我的项目中没有 log4j.xml,但这应该没问题,因为我只想使用 Spring AOP 概念在控制台中记录错误。

下面是我的代码,当我 运行 这个时,我可以在控制台中看到异常堆栈跟踪,但我没有看到我的 LoggingAspect.java 正在控制台中记录错误应该是。

我尝试在 LoggingAspect.java 中添加静态块以使用 System.out.println() 在控制台中打印一些文本,但它没有打印出来。

SpringConfig.java

package exercise5.com.aadi.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "exercise5.com.aadi.service")
public class SpringConfig {
}

LoggingAspect.java

package exercise5.com.aadi.utility;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class LoggingAspect {

    @AfterThrowing(pointcut = "execution(* exercise5.com.aadi.service.*Impl.*(..))", throwing = "exception")
    public void logExceptionFromService(Exception exception) throws Exception {
        Logger logger = LogManager.getLogger(this.getClass());
        logger.error(exception);
    }
}

我的异常来自 DAO

InsuranceServiceImpl.java

package exercise5.com.aadi.service;

...
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

...

@Service(value = "insuranceService")
public class InsuranceServiceImpl implements InsuranceService {

    ...

    @Override
    public List<PolicyReport> getReport(String policyType) throws Exception {
        ...

        if (filteredPolicy.isEmpty())
            throw new Exception("Service.NO_RECORD");

        ...

    }

    ...
}

下面是我收到的控制台消息

ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
Exception in thread "main" java.lang.Exception: Service.NO_RECORD
    at exercise5.com.aadi.service.InsuranceServiceImpl.getReport(InsuranceServiceImpl.java:43)
    at exercise5.com.aadi.ui.UserInterface.generateReport(UserInterface.java:45)
    at exercise5.com.aadi.ui.UserInterface.main(UserInterface.java:20)

但我期待的是

ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
Exception in thread "main" 02:03:52.656 [main] ERROR exercise5.com.aadi.service.InsuranceServiceImpl
java.lang.Exception: Service.NO_RECORD
    at exercise5.com.aadi.service.InsuranceServiceImpl.getReport(InsuranceServiceImpl.java:56) [bin/:?]
    at exercise5.com.aadi.ui.UserInterface.generateReport(UserInterface.java:45) [bin/:?]
    at exercise5.com.aadi.ui.UserInterface.main(UserInterface.java:20) [bin/:?]
java.lang.Exception: Service.NO_RECORD
    at exercise5.com.aadi.service.InsuranceServiceImpl.getReport(InsuranceServiceImpl.java:56)
    at exercise5.com.aadi.ui.UserInterface.generateReport(UserInterface.java:45)
    at exercise5.com.aadi.ui.UserInterface.main(UserInterface.java:20)

请注意,异常日志应该存在两次。第一个来自 Spring AOP LoggingAspect.java,第二个是正常的异常堆栈跟踪。

任何人都可以帮助我,为什么我没有得到第一个?

您正在指定

@ComponentScan(basePackages = "exercise5.com.aadi.service")

这意味着您的 LoggingAspect @Component 不会被 Spring 接收,因为它位于

之下
exercise5.com.aadi.utility

除此之外,您的 AOP 配置似乎是正确的。