我是Spring的新手,并尝试使用log4j实现Spring AOP来记录控制台中的错误。请注意,我的项目中没有log4j.xml,但这应该没问题,因为我只想使用Spring AOP概念在控制台中记录错误。
下面是我的代码,当我运行它时,我可以在控制台中看到Exception堆栈跟踪,但我没有看到我的LoggingAspect.java正在控制台中记录错误。
我尝试在LoggingAspect.java中添加一个静态块,使用System.out.println()在控制台中打印一些文本,但它不打印。
spring config.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 {
}
logging aspect.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
insurance service imp了.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,第二个是正常的Exception堆栈跟踪。
任何人都可以帮助我,为什么我没有得到第一个?
你指的是
@ComponentScan(basePackages = "exercise5.com.aadi.service")
这意味着你的LoggingAspect
@Component
将不会被Spring接收,因为它生活在
exercise5.com.aadi.utility
除此之外你的AOP配置似乎有点。