在eclipse里使用log4j记录程序日志

类别:Java 点击:0 评论:0 推荐:

1.下载eclipse和log4j相关软件
2.在eclipse中建立一个java项目,比如HelloEclipse,在此过程中add 外部jar log4j.jar文件
3。利用eclipse的import功能引入log4j.properties文件,并且进行修改,log4j在你下载的log4j包的example/lf/lf5里就有
如下:


# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.

# The root category uses the appender called A1. Since no priority is
# specified, the root category assumes the default priority for root
# which is DEBUG in log4j. The root category is the only category that
# has a default priority. All other categories need not be assigned a
# priority in which case they inherit their priority from the
# hierarchy.
# 如果是弹出log4j log记录器
#log4j.rootCategory=, A1 
# 直接在eclipse控制台中显示log纪录
log4j.rootCategory=, B1

# A1 is set to be a LF5Appender which outputs to a swing
# logging console.
 
#log4j.appender.A1=org.apache.log4j.lf5.LF5Appender
#log4j.appender.A1.MaxNumberOfRecords=1000

log4j.appender.B1=org.apache.log4j.ConsoleAppender
log4j.appender.B1.layout=org.apache.log4j.PatternLayout
log4j.appender.B1.layout.ConversionPattern=%-4r % -5p [%t] % 37c %3x - %m%n
log4j.appender.B1.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n

4。写HelloEclipse代码如下:


import org.apache.log4j.*;
class HelloEclipse{
 static Logger logger=Logger.getLogger(HelloEclipse.class.getName());
 public static void main(String args[])
 {
    PropertyConfigurator.configure("Log4j.properties");
    HelloEclipse.logger.info("纪录测试"); 
    int valueoflover=1;
    HelloEclipse.logger.info("valueoflover" + valueoflover);
 }
}

运行,就可以看到如下log信息了

 INFO [main] (HelloEclipse.java:7) -纪录测试
 INFO [main] (HelloEclipse.java:9) -valueoflover1

请参考:http://blog.csdn.net/jemlee2002/archive/2004/10/10/Jem05.aspx

本文地址:http://com.8s8s.com/it/it14369.htm