2011-02-26 72 views
1

我想使用公共日誌記錄,並希望使用java.util.logging作爲基礎機制。公共日誌記錄使用java.util.logging

LogTest.java

import org.apache.commons.logging.*; 

public class LogTest { 

     public static void main(String args[]) { 
      System.setProperty("java.util.logging.config.file","log.properties"); 
      Log logger = LogFactory.getLog(LogTest.class); 
      logger.trace("trace msg"); 
     } 
} 

我的src /主/資源/ log.properties(我使用Maven項目)

handlers=java.util.logging.ConsoleHandler 

# Default global logging level. 
# Loggers and Handlers may override this level 
.level=ALL 

我無法看到任何輸出。 請告訴我如何以編程方式設置log.properties以利用java日誌記錄。

回答

0
# Loggers and Handlers may override this level 

嘗試在您的log.properties中添加java.util.logging.ConsoleHandler.level=ALL

+0

對不起。這沒有奏效。和我的內容src/main/resources/commons-logging.properties:org.apache.commons.logging.Log = org.apache.commons.logging.impl.Jdk14Logger – 2011-02-27 06:38:41

3

您需要指定一個實現Log接口的記錄器。在這種情況下,Jdk14Logger

查看更多詳情How to use 'Apache Commons Logging' with 'Java SE Logging'?

除了從鏈接:

PUT 「commons-logging.properties」 的文件在你的應用的classpath。 此文件的內容應該如下所示: org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger 設置此屬性可指示Commons-Logging使用JDK Logger (即Java SE Logger)作爲Log實現。

然後把log-config.properties放在你的類路徑中並進行相應的配置。

如果您決定在將來更改impls,more loggers are available out of the box

+0

感謝您分享我的博客鏈接! – 2014-08-25 00:16:42

+0

這可以工作(一個簡單的Java項目)。但是在Eclipse/STS中與Maven項目一樣的嘗試不會。嘗試將日誌配置文件(我將所有配置放入commons-logging.properties本身內)放入「src/main/resources」和「src/main/java」以及項目的根目錄中,但沒有被拾取。在這種情況下,使用maven時類路徑是什麼? – user104309 2016-09-18 16:24:14

+0

'src/main/resources'應該是正確的。如果它不起作用,你需要驗證你對classpath中包含的內容的假設。您可能還想驗證一切打包是否正確打開你的jar/war來確保文件在你期望的位置。 – Snekse 2016-09-19 17:04:33

0

創建commons-logging.properties在類路徑:

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl 
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger 

設置java.util.logging.config.file您之前記錄器的實例:

System.setProperty("java.util.logging.config.file","log.properties"); 

則JDK日誌記錄將爲appender org.apache.commons.logging。*所使用的所有日誌。

示例:spring,hibernate,struts等