`
yhef
  • 浏览: 66995 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Eclipse集成Log4j

阅读更多
一、准备:
1:下载log4j
http://logging.apache.org/log4j/1.2/download.html
http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip

二、集成:

1:把log4j-1.2.16.jar放入CLASSPATH中




2:在Eclipse中建立一个源码包config,将log4j.properties拷贝进去
此文件可在本文的附件或以下目录找到
xxxxxx\apache-log4j-1.2.16\examples\lf5\InitUsingLog4JProperties\log4j.properties



3:在D盘建立一个放日志文件的目录D:\eclipse_log,以及文件tools.log
上面的文件路径是写在Properties文件中,如果你的不一样,或不用输出到日志文件,请自行修改或跳过。

4:编辑Properties文件内容

附件中有一个简单的Properties文件,关于Properties内容的编写请参考google.

5:编写一个简单的执行类



package com.yhef.tools.test;

public class ListTest {

	
	public static void main(String[] args) {
		Logger logger = Logger.getLogger(ListTest.class);
		logger.debug("this is debug level");
		logger.info("this is info level");
		logger.warn("this is warn level");
		logger.error("this is error level");
		logger.fatal("this is fatal level");		
	}

	

}


6:执行class。
如果正常,Eclipse的控制台会打印出类似
[YHEF-TOOLS] 2010-08-14 15:08:34,593 DEBUG [main] com.yhef.tools.test.ListTest.testListIsBlank(11) | this is debug level
[YHEF-TOOLS] 2010-08-14 15:08:34,609 INFO [main] com.yhef.tools.test.ListTest.testListIsBlank(12) | this is info level
[YHEF-TOOLS] 2010-08-14 15:08:34,609 WARN [main] com.yhef.tools.test.ListTest.testListIsBlank(13) | this is warn level
[YHEF-TOOLS] 2010-08-14 15:08:34,609 ERROR [main] com.yhef.tools.test.ListTest.testListIsBlank(14) | this is error level
[YHEF-TOOLS] 2010-08-14 15:08:34,609 FATAL [main] com.yhef.tools.test.ListTest.testListIsBlank(15) | this is fatal level



D:\eclipse_log\tools.log文件里应该也有相应的输出。

常见错误可以参考:
http://logging.apache.org/log4j/1.2/faq.html
  • 大小: 63.5 KB
  • 大小: 10.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics