关于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. Python操作Influxdb数据库

    1.influxdb基本操作[root@test ~]# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.4.x86_64. ...

  2. 自己动手编写IPv4地址包含关系测试的小脚本

    工作中需要对地址包含关系进行测试,现有ipaddress标准库和IPy无法满足,于是自己动手编写小脚本,主要实现== , in, <等专用功能,其他功能可以后续用到再补充,例如迭代打印网段内所有 ...

  3. spring boot(十八)集成FastDFS文件上传下载

    上篇文章介绍了如何使用Spring Boot上传文件,这篇文章我们介绍如何使用Spring Boot将文件上传到分布式文件系统FastDFS中. 这个项目会在上一个项目的基础上进行构建. 1.pom包 ...

  4. win7 64安装msyql

    https://www.cnblogs.com/orrz/p/5456247.html 1:下载安装包,等待等待的过程,OK,在短暂的等待后,下载完成,解压先,简单起见,解压到D盘吧,D:\mysql ...

  5. 让你明白kvm是什么

    参考:https://blog.csdn.net/bbwangj/article/details/80465320 KVM 工具集合: libvirt:操作和管理KVM虚机的虚拟化 API,使用 C ...

  6. 6月5 Smarty自定义函数

    自定义函数:<{方法名称}> 在html页面是可以直接赋值的:(没啥作用只是知道即可) <{$a = "hello"}><div><{$a ...

  7. Web App和Native App的比较

    一.Web App vs. Native App 比起手机App,网站有一些明显的优点. 跨平台:所有系统都能运行 免安装:打开浏览器,就能使用 快速部署:升级只需在服务器更新代码 超链接:可以与其他 ...

  8. Django之缓存

    由于Django构建得是动态网站,每次客户端请求都要严重依赖数据库,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...

  9. 解决linux环境下qt groupbox 边框不显示问题

    ps:实践是检验真理的唯一标准真的是没错,以为很简单一件事情,往往被自己搞的很复杂,这里记录下 在windows环境中Qt创建一个groupbox自动显示边框, 效果如下 然而在linux环境中Qt创 ...

  10. 小程序BindTap快速连续点击页面跳转多次

    原因: 手机端点击Tap基础事件解决300ms延迟 解决办法: success 里面加一个延迟300ms能解决 setTimeout goRob(e) { const that = this retu ...