关于Log4j的简单示例
<!--手动配置log4j.properties文件内容:-->
1 #level:debug/info/warn/error
log4j.rootLogger=OFF,stdout,file
log4j.logger.isoft=ERROR,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.layout=org.apache.log4j.SimpleLayout
log4j.appender.file.File=D: \\error.log
<!--log4j.proerties由tomcat自动获取内容,不需要用户代码实现-->
<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--bean...-->
<bean id="exceptionlogger" class="isoft.aop.ExceptionLogger"></bean> <!-- 定义方法的切入点 -->
<aop:config>
<aop:pointcut id="servicepointcut"
expression="execution(* isoft.serviceImpl.*.*(..))"></aop:pointcut>
<aop:aspect id="loggeraspect" ref="exceptionlogger">
<aop:after-throwing throwing="e" method="loggerException"
pointcut-ref="servicepointcut"/>
</aop:aspect>
</aop:config>
</beans>
import java.util.Date;
import org.apache.log4j.Logger;
/**
* 批注:
* org.apache.log4j.Logger中包含日志相关函数,
* 存在于log4j-xxx.jar中
*/
/**
* @author Sufeng
*
*/
/*descripation:该类是在发生异常时触发
* 保存到错误信息到指定目录
*/
public class ExceptionLogger {
Logger logger=logger = Logger.getLogger(ExceptionLogger.class);
public void loggerException(Exception e) {
StringBuilder Buf=new StringBuilder();
Buf.append("================="+new Date()+"==============");
StackTraceElement[] elements=e.getStackTrace();
for(StackTraceElement element:elements) {
Buf.append(element+"\n");
}
logger.error(Buf.toString());
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import isoft.service.UserService;
public class Test {
private static final String CONFIG = "aop.xml";
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(CONFIG);
UserService userService = (UserService) ac.getBean("userserviceImpl");
userService.update();
userService.save();
userService.delete();
}
}