log4j学习(三)——AOP日志

上篇讲到了,通过log4j在自己计算机上生成日志文件。这篇主要是和spring的AOP整合起来,将日志作为一个切面横切到系统中。

一)、相关知识:

Log4j:

Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码(百度百科)。

AOP:

面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点。

二、具体实现

1、总体结构

2、Dao层

  UserDao   和  GroupDao

public interface UserDao {	

	public void addUser();

	public void deleteUser();

	public void updateUser();
}

public interface GroupDao {

	public void addGroup();

	public void deleteGroup();

	public void updateGroup();
}

UserDaoImpl   和  GroupDaoImpl

public class UserDaoIml implements UserDao {

	public void addUser() {
		int i = 0;
		int n = 2 / 0;
		System.err.println("save user to database");
	}

	public void deleteUser() {
		System.err.println("delete user from database");
	}

	public void updateUser() {
		System.err.println("update user to database");
	}
}

public class GroupDaoIml implements GroupDao {

	public void addGroup() {
		System.out.println("save Group to database");

	}

	public void deleteGroup() {
		System.out.println("delete Group to database");

	}

	public void updateGroup() {
		System.out.println("update Group to database");

	}

}

3、Service层

UserService    和  GroupService

public interface UserService {

	public void addUser();

	public void deleteUser();

	public void updateUser();
}
public interface GroupService {

	public void addGroup();

	public void deleteGroup();

	public void updateGroup();
}

     UserServiceImpl    和  GroupServiceImpl

		public class UserServiceIml implements UserService {

			private UserDao userDao;

			public UserDao getUserDao() {
				return userDao;
			}

			public void setUserDao(UserDao userDao) {
				this.userDao = userDao;
			}

			public void addUser() {
				userDao.addUser();
			}

			public void deleteUser() {
				userDao.deleteUser();
			}

			public void updateUser() {
				userDao.updateUser();
			}

		}

		public class GroupServiceIml implements GroupService {

			private GroupDao groupDao;

			public GroupDao getGroupDao() {
				return groupDao;
			}

			public void setGroupDao(GroupDao groupDao) {
				this.groupDao = groupDao;
			}

			public void addGroup() {
				groupDao.addGroup();

			}

			public void deleteGroup() {
				groupDao.deleteGroup();
			}

			public void updateGroup() {
				groupDao.updateGroup();

			}

		}

4、Logger横切面

public class UserLog {

	Logger logger = Logger.getLogger(UserLog.class);

	public void begin(JoinPoint joinPoint) {

		logger.info("开始执行-------------" + joinPoint.getTarget().getClass()
				+ "." + (joinPoint.getSignature().getName()));

	}

	public void excption(JoinPoint joinPoint, Throwable ex) {
		logger.error("出现异常-------------" + joinPoint.getTarget().getClass()
				+ "." + joinPoint.getSignature().getName() + "-----异常信息:"
				+ ex.getMessage() +"\r\n"+ getExceptionTrace(ex));
	}

	public void end(JoinPoint joinPoint) {

		logger.info("执行完毕-------------" + joinPoint.getTarget().getClass()
				+ "." + (joinPoint.getSignature().getName()));
	}

	public static String getExceptionTrace(Throwable ex) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		ex.printStackTrace(pw);
		return sw.toString();

	}

}

5、log4j.properties

log4j.rootLogger=Info,info,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{yyy-MM-dd-HH:mm:ss}] - %m %l %n 

log4j.logger.info=Info,info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=[%-5p] [%d{yyy-MM-dd-HH:mm:ss}] - %m %l %n 
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.File=D\:/SystemInfo.log

6、Application.xml

	<bean id="groupDao" class="com.tgb.dao.GroupDaoIml">
	</bean>
	<bean id="groupService" class="com.tgb.service.GroupServiceIml"
		scope="prototype">
		<property name="groupDao" ref="groupDao"></property>
	</bean>

	<bean id="userDao" class="com.tgb.dao.UserDaoIml">
	</bean>
	<bean id="userService" class="com.tgb.service.UserServiceIml"
		scope="prototype">
		<property name="userDao" ref="userDao"></property>
	</bean>

	<bean id="userLog" class="com.tgb.dao.UserLog"></bean>

	<aop:config>
		<!-- 定义一个切面 -->
		<aop:aspect ref="userLog">
			<!-- 切点 -->
			<aop:pointcut expression="execution(* com.tgb.dao.*.*(..))"	id="add" />
			<!-- 方法执行之前 -->
			<aop:before method="begin" pointcut-ref="add" />
			<!-- 方法执行之后 -->
			<aop:after method="end" pointcut-ref="add" />
			<!-- 出现异常 -->
			<aop:after-throwing pointcut-ref="add" method="excption" throwing="ex" />
		</aop:aspect>
	</aop:config>
</beans>

7、测试

@Test
public void testAddUser() {
      ApplicationContext aContext = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
	UserService userService = (UserService) aContext.getBean("userService");
	userService.deleteUser();
	userService.addUser();

	GroupService groupService = (GroupService) aContext.getBean("groupService");
	groupService.updateGroup();
	groupService.deleteGroup();

}

8、结果

控制台输出:

[INFO ] [2015-03-28-12:27:19] - Refreshing org.[email protected]490d6c15: startup date [Sat Mar 28 12:27:19 CST 2015]; root of context hierarchy org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:495)
 [INFO ] [2015-03-28-12:27:19] - Loading XML bean definitions from file [C:\Users\Lei\Workspaces\MyEclipse10\AopLog\WebRoot\WEB-INF\applicationContext.xml] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315)
 [INFO ] [2015-03-28-12:27:20] - Pre-instantiating singletons in org.s[email protected]68bbe345: defining beans [groupDao,groupService,userDao,userService,userLog,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,add]; root of factory hierarchy org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.begin(UserLog.java:20)
 update Group to database
[INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.begin(UserLog.java:20)
 delete Group to database
[INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.begin(UserLog.java:20)
 delete user from database
[INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.begin(UserLog.java:20)
 [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.end(UserLog.java:33)
 [ERROR] [2015-03-28-12:27:21] - 出现异常-------------class com.tgb.dao.UserDaoIml.addUser
异常信息:java.lang.ArithmeticException: / by zero
	at com.tgb.dao.UserDaoIml.addUser(UserDaoIml.java:7)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy3.addUser(Unknown Source)
	at com.tgb.service.UserServiceIml.addUser(UserServiceIml.java:19)
	at com.tgb.test.TestUser.testAddUser(TestUser.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
 com.tgb.dao.UserLog.excption(UserLog.java:26) 

D盘SystemInfo.log

[INFO ] [2015-03-28-12:27:19] - Refreshing org.[email protected]490d6c15: startup date [Sat Mar 28 12:27:19 CST 2015]; root of context hierarchy org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:495)
 [INFO ] [2015-03-28-12:27:19] - Loading XML bean definitions from file [C:\Users\Lei\Workspaces\MyEclipse10\AopLog\WebRoot\WEB-INF\applicationContext.xml] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315)
 [INFO ] [2015-03-28-12:27:20] - Pre-instantiating singletons in org.s[email protected]68bbe345: defining beans [groupDao,groupService,userDao,userService,userLog,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,add]; root of factory hierarchy org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.begin(UserLog.java:20)
 [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.updateGroup com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.begin(UserLog.java:20)
 [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.GroupDaoIml.deleteGroup com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.begin(UserLog.java:20)
 [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.deleteUser com.tgb.dao.UserLog.end(UserLog.java:33)
 [INFO ] [2015-03-28-12:27:21] - 开始执行-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.begin(UserLog.java:20)
 [INFO ] [2015-03-28-12:27:21] - 执行完毕-------------class com.tgb.dao.UserDaoIml.addUser com.tgb.dao.UserLog.end(UserLog.java:33)
 [ERROR] [2015-03-28-12:27:21] - 出现异常-------------class com.tgb.dao.UserDaoIml.addUser
异常信息:java.lang.ArithmeticException: / by zero
	at com.tgb.dao.UserDaoIml.addUser(UserDaoIml.java:7)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy3.addUser(Unknown Source)
	at com.tgb.service.UserServiceIml.addUser(UserServiceIml.java:19)
	at com.tgb.test.TestUser.testAddUser(TestUser.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
 com.tgb.dao.UserLog.excption(UserLog.java:26) 

(完)

三、小结

  AOP作为对OOP的一个补充,使得那些独立于业务之外的公共服务例如日志,事务,安全验证等横切过来,从而实现了与业务的松耦合。再结合自定义log4j属性文件,从而AOP的实现系统的日志记录服务!

时间: 2024-11-08 23:35:27

log4j学习(三)——AOP日志的相关文章

spring框架学习(三)——AOP( 面向切面编程)

AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 所谓的周边功能,比如性能统计,日志,事务管理等等 周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP 原理图 1. 功能分两大类,辅助功能和

log4j(三)——如何控制不同级别的日志信息的输出?

一:测试环境与log4j(一)--为什么要使用log4j?一样,这里不再重述 二:老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; public class UseLog4j { //日志记录器 private static Logger LOGGER = LogManager.getLogger(UseLog4j.class); //程序入口--主函数 public static void main(String[]args){ //设置日志信息的格式化

Log4J学习【一】Log4J简介

Log4J简介 基本上所有的大型应用,包括我们常用的框架,比如hibernate:spring:struts等,在其内部都做了一定数量的日志信息.为什么要做这些日志信息,在系统中硬编码日志记录信息是调试系统,观察系统运行状态的一种方式.可能大部分程序员都还记得自己最开始写代码的时候,写一个方法总是出错,就喜欢使用System.out.println("1111111")之类的代码来查看程序的运行是否按照自己想要的方式在运行,其实这些sysout就是日志记录信息,但使用System.ou

log4j学习总结

log4j是一个优秀的日志组件,基本上所有的java开发项目都会用到它.下面将自己学习的一些心得总结一下,方便以后学习. log4j在项目中都不会单独使用,至少是我写过的java项目中没有.一般来说log4j是和commons-logging组件一同使用. commons-logging和log4j的关系就像是jdbc和mysql数据库一样,一个是工厂方法,一个是具体的实现,好处:1,逻辑代码是实现分离2.维护变少.其中第二点是很重要的.配置比单独使用log4j组件还要简单. 先说一下原理. 项

spring aop 日志 注解

前提:接上篇:spring aop 日志 一:无新增依赖包 二:修改切面类(aop.Log4jHandlerAOP.java) 三:修改applicationContext.xml 1:扫描注入容器新增aop(base-package="dao,service,aop") 2:支持AOP注解(<aop:aspectj-autoproxy/>) 3:去除传统AOP的配置以及切面类的注入 ps: 1:applicationContext.xml <?xml version

原创Log4j学习----会飞的猪

在前面,我们学习到了关于Log4j的一些知识,如它的构成由三大重要组成部分构成,Logger.Appenders.Layout.相关的作用,这里就不一一介绍了. 如Logger:控制要启用或禁用哪些日志记录语句,并对日志信息进行级别限制:Appenders指定了日志将打印到控制台还是文件中;而Layout则控制日志信息的显示格式. 下面简单介绍下,如何创建Logger对象及配置log4j.properties文件. A).Logger对象的获得或创建:            Logger被指定为

为什么使用 SLF4J 而不是 Log4J 来做 Java 日志

转自:为什么使用 SLF4J 而不是 Log4J 来做 Java 日志 英文原文:Why use SLF4J over Log4J for logging in Java 每个Java开发人员都知道日志记录对Java应用的重要性,尤其是对服务端应用,而且其中许多人都已经熟悉了各种记录日志的库,比如java.util.logging,Apache的log4j,logback,然而如果你不知道SLF4J,java的简单记录日志的设计的话 ,那么到了学习并在你的项目中使用它的时候了.在这篇Java文档

JAVA中使用log4j及slf4j进行日志输出的方法

JAVA中输出日志比较常用的是log4j,这里讲下log4j的配置和使用方法,以及slf4j的使用. 一.下载log4j的架包,并导入项目中,如下: 二.创建log4j.properties配置文件 1.log4j配置文件的位置: (1)如果是java project项目,则在项目的根目录下创建log4j.properties而不是在src目录下. (2)如果是java web项目,则在src目录下创建log4j.properties配置文件,因为这个时候tomcat会去默认的加载这个配置文件,

从头认识Spring-3.1 简单的AOP日志实现-某方法之前的前后记录日志

这一章节我们引入简单的AOP日志实现. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1_1; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } } 烤炉类:

log4j学习二-log4j认识

log4j(标准日志工具)三大组件: logger appender layout 例如: ? 1 2 3 log4j.logger.myTest2=INFO,B   log4j.appender.B=org.apache.log4j.ConsoleAppender   log4j.appender.B.layout=org.apache.log4j.PatternLayout  日志级别: off---------------------------------最高等级的,用于关闭所有日志记录