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

Log4j 日志记录的更多相关文章

  1. ELK环境配置+log4j日志记录

    ELK环境配置+log4j日志记录 1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一 ...

  2. Hibernate 使用log4j日志记录

    日志记录使程序员能够将日志详细信息永久写入文件.这是我们以后在开发当中非常重要的一步. Hibernate使用log4j日志记录,我们需要以下几个步骤: 1.导入jar包: (1)这是hibernat ...

  3. java Web应用配置log4j日志记录

    公司启动全国支票影像系统,有这样一个功能是和消息中间件打交道,需要记录每一个报文出错情况,因为方便后期开发人员的调试,我选用了log4j作为日志记录,好了废话不多说了! 第一步:首先建立一个WEB工程 ...

  4. log4j日志记录级别是如何工作?

    级别p的级别使用q,在记录日志请求时,如果p>=q启用.这条规则是log4j的核心.它假设级别是有序的.对于标准级别它们关系如下:ALL < DEBUG < INFO < WA ...

  5. ELK菜鸟手记 (一) 环境配置+log4j日志记录

    1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一个关键字查询日志中某个关键信息相当困 ...

  6. 在android中配置 slf4j + log4j 日志记录框架

    需求: 在项目开发中,需要记录 操作日志 .起初自己写了个简单的日志记录文本写入到文本的方法,后来随着项目的膨胀,需要考虑更多的操作,开始考虑性能问题. 实现: 考虑使用 slf4j + log4j ...

  7. Hibernate使用Log4j日志记录(使用properties文件)

    我们知道,Log4j和Logback框架可用于支持日志记录hibernate,使用log4j有两种执行日志记录的方法: 通过log4j.xml文件(或) 通过log4j.properties文件 在这 ...

  8. Hibernate使用Log4j日志记录(使用xml文件)

    日志记录使程序员能够将日志详细信息永久写入文件. Log4j和Logback框架可以在hibernate框架中使用来支持日志记录. 使用log4j执行日志记录有两种方法: 通过log4j.xml文件( ...

  9. Log4j日志记录

    1.导入log4j的jar包 2.写log4j.properties文件,配置日志记录参数,一般参数如下所示: 第二行指定了输出日志的目录,此处用的相对路径,也可换成绝对路径: 第三行指定了输出的记录 ...

  10. SpringBoot-整合log4j日志记录

    新建log4j配置文件 文件名称log4j.properties #log4j.rootLogger=CONSOLE,info,error,DEBUG log4j.rootLogger=info,er ...

随机推荐

  1. 单分子荧光原位杂交(smFISH)

    single-molecule RNA fluorescence in situ hybridization (RNA smFISH) 单分子荧光原位杂交(smFISH)是一种新的基因表达分析方法,能 ...

  2. 2017-2018-2 20165303 实验二《Java面向对象程序设计》实验报告

    实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况, ...

  3. 在linux中要直接查找的命令

  4. Python安装第三方库,报错超时: Read timed out.

    1.安装beautifulsoup4 >pip install beautifulsoup4 报错超时: Read timed out. 2.解决办法:pip --default-timeout ...

  5. JavaScript基础一

    1.1 javascript简介 Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) J ...

  6. 把url链接转换成二维码的工具类

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io ...

  7. PostgreSQL CPU占用100%性能分析及慢sql优化

    查看连接数变化 CPU利用率到达100%,首先怀疑,是不是业务高峰活跃连接陡增,而数据库预留的资源不足造成的结果.我们需要查看下,问题发生时,活跃的连接数是否比平时多很多.对于RDS for PG,数 ...

  8. InnoDB存储引擎介绍-(4)Checkpoint机制二

    原文链接 http://www.cnblogs.com/chenpingzhao/p/5107480.html 一.简介 思考一下这个场景:如果重做日志可以无限地增大,同时缓冲池也足够大,那么是不需要 ...

  9. Activiti手动执行的应用(UserTask)

    工作流模拟某公司请假流程情景如下:   1.开发人员请假流程,如果开发人员请假,如果请假天数小于3天,组长批准,人事批准即可请假.   2.如果请假大约三天,需要项目经理或者产品经理批准,并且项目总监 ...

  10. KM算法详解[转]

    KM算法详解 原帖链接:http://www.cnblogs.com/zpfbuaa/p/7218607.html#_label0 阅读目录 二分图博客推荐 匈牙利算法步骤 匈牙利算法博客推荐 KM算 ...