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

时间:2019-03-24 20:54:53

标签: java spring logging spring-aop

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

下面是我的代码,当我运行此代码时,我可以在控制台中看到Exception堆栈跟踪,但是我没有看到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,第二个是普通的Exception堆栈跟踪。

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

1 个答案:

答案 0 :(得分:0)

您要指定

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

这意味着您的LoggingAspect @Component不会被Spring接走,因为它生活在

exercise5.com.aadi.utility

除了您的AOP配置似乎是正确的。