2016-02-27 53 views
0

我想要的log4j添加到我的web應用程序來記錄服務器和客戶端之間的交流,所以我說在我的pom.xml的依賴:如何log4j的添加到Web應用程序在Java中

<dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.17</version> 
    </dependency> 

和我創建了log4j.proporties:

# Root logger option 
    log4j.rootLogger=INFO, file 

    # Direct log messages to a log file 
    log4j.appender.file=org.apache.log4j.RollingFileAppender 

    #Redirect to Tomcat logs folder 
    #log4j.appender.file.File=${catalina.home}/logs/logging.log 

    log4j.appender.file.File=C:\\test.log 
    log4j.appender.file.MaxFileSize=10MB 
    log4j.appender.file.MaxBackupIndex=10 
    log4j.appender.file.layout=org.apache.log4j.PatternLayout 
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m% 

和下面是我的代碼:

static final Logger logger = Logger.getLogger(hellologger.class); 
public static void main(String[] args) { 
String log4JPropertyFile = "C:\\Users\\xxxx\\Desktop\\log4j.properties"; 
Properties p = new Properties(); 

    try { 
     p.load(new FileInputStream(log4JPropertyFile)); 
     PropertyConfigurator.configure(p); 
     logger.info("Wow! I'm configured!"); 
    } catch (IOException e) { 


    } 
    } 

但最終我得到這個錯誤:

Infos: org.osgi.framework.BundleException: Unresolved constraint in bundle com.mycompany.webclient [329]: Unable to resolve 329.7: missing requirement [329.7] osgi.wiring.package; (&(osgi.wiring.package=org.apache.log4j)(version>=1.2.0)(!(version>=2.0.0))) 
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974) 
at org.apache.felix.framework.Felix.startBundle(Felix.java:2037) 
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955) 
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1175) 
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:1153) 
at org.apache.felix.fileinstall.internal.DirectoryWatcher.processAllBundles(DirectoryWatcher.java:1146) 
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:456) 
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:263) 

任何想法如何解決這個問題?

+0

請你發佈您的代碼,其中u嘗試做的log4j的自動裝配。您必須檢查.m2,這是您的log4j.jar可用或不可用?如果存在然後刪除這個jar並重新編譯'pom.xml'以添加依賴項 –

+0

檢查更新後的文章:)你能否向我解釋更多如何檢查log4j.jar – foucha

+0

去你的用戶文件夾中有.m2文件夾。你可以看看這個(Windows操作系統) –

回答

0

你可以通過關於log4j的this教程。

正確解釋了屬性文件和pom.xml文件。產生

日誌是這樣的:

2014-07-02 20:52:39 DEBUG HelloExample:19 - This is debug : mkyong 
2014-07-02 20:52:39 INFO HelloExample:23 - This is info : mkyong 
2014-07-02 20:52:39 WARN HelloExample:26 - This is warn : mkyong 
2014-07-02 20:52:39 ERROR HelloExample:27 - This is error : mkyong 
2014-07-02 20:52:39 FATAL HelloExample:28 - This is fatal : mkyong 
+0

我已經遵循這個教程:p – foucha

相關問題