gpt4 book ai didi

java - AOP日志记录: @Aspect is not logging the error in console for log4j default configuration

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:48 24 4
gpt4 key购买 nike

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

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

我尝试在 LoggingAspect.java 中添加一个静态 block ,以使用 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 配置似乎很正确。

关于java - AOP日志记录: @Aspect is not logging the error in console for log4j default configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com