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

来源:互联网 发布:网络绿肥是什么意思 编辑:程序博客网 时间:2024/05/22 12:54
在eclipse里使用log4j记录程序日志
文章出处:华军   发布时间:2005-10-26
<script type="text/javascript"><!--google_ad_client = "pub-4874677934472477";google_ad_width = 250;google_ad_height = 250;google_ad_format = "250x250_as";google_ad_type = "text";google_ad_channel ="";google_color_border = "F6F6F6";google_color_bg = "F6F6F6";google_color_link = "000000";google_color_url = "0066FF";google_color_text = "000000";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

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

 
原创粉丝点击