Log4j 日志记录

关于Log4j的简单示例

<!--手动配置log4j.properties文件内容:-->
1
#level:debug/info/warn/error 2 log4j.rootLogger=OFF,stdout,file 3 log4j.logger.isoft=ERROR,file 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout 6 log4j.appender.file=org.apache.log4j.FileAppender 7 log4j.appender.file.layout=org.apache.log4j.SimpleLayout 8 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();
    }
}

 

转载于:https://www.cnblogs.com/Su-feng-address/p/9945372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值