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();
}