Eclipse集成Log4j

一、准备:
1:下载log4j
[url]http://logging.apache.org/log4j/1.2/download.html[/url]
[url]http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip[/url]

二、集成:

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

[img]http://dl.iteye.com/upload/attachment/292910/a5924893-7f4e-3635-b5a7-cff931bdb963.jpg[/img]


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

[img]http://dl.iteye.com/upload/attachment/292912/3096d0b7-914d-3ea3-987b-0648b7971378.jpg[/img]

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文件里应该也有相应的输出。

常见错误可以参考:
[url]http://logging.apache.org/log4j/1.2/faq.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值