spring用aop记录异常日志

本文介绍如何使用Log4j进行日志配置,并通过Spring AOP实现异常信息的日志记录。具体步骤包括引入Log4j依赖、创建配置文件、编写测试类、定义异常处理Aspect等方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入log4j的jar包


2.在src目录下新建log4j.properties文件


3.在log4j.properties文件中写


#--------console(将日志输出在控制台)-----------
log4j.rootLogger=warn,myconsole
log4j.appender.myconsole=org.apache.log4j.ConsoleAppender
log4j.appender.myconsole.layout=org.apache.log4j.SimpleLayout
#---------file(输出在文件中)------------
log4j.rootLogger=warn,myfile
log4j.appender.myfile=org.apache.log4j.FileAppender
log4j.appender.myfile.File=D:\\log.txt
log4j.appender.myfile.layout=org.apache.log4j.SimpleLayout
#---------file(输出在html中)------------
log4j.rootLogger=warn,myfile
log4j.appender.myfile=org.apache.log4j.FileAppender
log4j.appender.myfile.File=E\:\\error.htm
log4j.appender.myfile.layout=org.apache.log4j.HTMLLayout


4.测试


public class Test2 {
    static Logger logger =   Logger.getLogger(Test2 .class);
    public static void main(String[] args) {
        logger.debug("调试信息");
        logger.info("普通信息");
        logger.warn("警告信息");
        logger.error("错误信息");
        logger.fatal("致命信息");
    }
}


5.写异常处理的aspect方面类 ExceptionLogger


6.类中写方法


    public void mypoint(){
    }
    /**
     * 将异常信息记录到文件中
     * @param ex 目标对象方法抛出的异常对象
     */
    public void execute(Exception ex){
        System.out.println("====记录异常信息=====");
        StackTraceElement[] els = ex.getStackTrace();
        Logger logger = Logger.getLogger(this.getClass());
        logger.error(ex.getClass().getName());
        logger.error(els[0]);//利用Log4j工具输出错误
    }


7.spring配置文件中加入


    <!-- 异常处理 -->
<bean id="exceptionBean"  class="aop.ExceptionLogger"></bean>

<aop:aspect id="exceptionAspect"  ref="exceptionBean">
            <aop:after-throwing method="execute"   pointcut-ref="servicePointcut"  throwing="ex"/>
</aop:aspect>

完整的配置文件例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

 <bean id="one" class="action.OneAction"></bean>
 
 <bean id="loggerBean" class="aop.LoggerBean" scope="prototype"
  lazy-init="true"></bean>
 <bean id="exceptionBean" class="aop.ExceptionLogger"></bean>

 <aop:config>
  <aop:pointcut id="servicePointcut" expression="within(action.*)" />
  <aop:aspect id="loggerAspect" ref="loggerBean">
   <aop:around method="loggerOperation" pointcut-ref="servicePointcut" />
  </aop:aspect>
  <aop:aspect id="exceptionAspect" ref="exceptionBean">
   <aop:after-throwing method="execute" pointcut-ref="servicePointcut"
    throwing="ex" />
  </aop:aspect>
 </aop:config>
 
</beans>

 

8.制造异常


throw new NullPointerException();

 

9.测试


 @Test
 public void testSpring(){
    String[] conf = {"applicationContext.xml"};
    ApplicationContext ac =new ClassPathXmlApplicationContext(conf);
    OneAction abc= (OneAction) ac.getBean("one");
     abc.execute();
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值