2013-03-30 192 views
0

我想在我的項目中使用org.apache.log4j.Logger。我有dowloaded log4j-1.2.17將jar包添加到我在Eclipse中引用的包中。我創建一個文件作爲配置文件在我的項目的根目錄(我認爲是CLASSPATH中'。'表示的當前目錄)。內容如下:配置log4j

log4j.rootLogger=info, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
# Pattern to output the caller's file name and line number. 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 

在我的項目:

import org.apache.log4j.Logger; 


public class Recommend {  

    private final static Logger log = Logger.getLogger(Recommend.class); 
    //... 
    public static void main(String[] args) { 
     log.info("Hello world!"); 
    } 

但它不會在控制檯打印信息,併產生一個警告而不是說:

log4j:WARN No appenders could be found for logger (main.Recommend). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

我怎樣才能使它工作?感謝您的關注!

+1

嘗試設置logj4性質src文件夾 –

+0

文件@AlekseiBulgak是的,最後..它的作品。所以這意味着當前目錄是src目錄而不是項目的根目錄。這是對的嗎? – zoujyjs

+0

@AlekseiBulgak但這沒有任何意義,因爲當我在我的代碼中使用相對路徑寫入文件時,當前目錄是根目錄... – zoujyjs

回答

1

確保log4j.properties文件位於類路徑中。在src文件夾

1

你的log4j.properties文件它的log4j.xml的工作great.replace只要把你的log4j.properties文件

enter image description here