2015-04-22 89 views
2

我有一個項目,在啓動SpringApplication之前需要日誌記錄機制。我怎樣才能做到這一點?Spring應用程序啓動之前的Spring啓動安裝日誌記錄

我試圖設置自己的日誌記錄機制(LogManager.getLogManager()。readConfiguration()),但它在Spring應用程序啓動時被覆蓋。

基本上我想在任何地方使用相同的日誌記錄機制。

回答

0

我設法從我的項目中刪除「彈簧引導啓動日誌記錄」的依賴,增加「組織來解決我的問題.slf4j:slf4j-jdk14:1.7.5'和'commons-logging:commons-logging:1.1.1'。

我使用gradle這個所以我來說,我做到這一點的:

compile("org.springframework.boot:spring-boot-starter-web") { 
    exclude module: "spring-boot-starter-logging" 
} 
compile("org.springframework.boot:spring-boot-starter-actuator") { 
    exclude module: "spring-boot-starter-logging" 
} 

compile('org.slf4j:slf4j-jdk14:1.7.5') 
compile('commons-logging:commons-logging:1.1.1') 

刪除LoggingApplicationListener和logback.xml沒有奏效。

0

您可以在web.xml代替彈簧的context.xml

<context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/log4j.properties</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

所以它是春天開始之前配置Log4j聽衆。

+0

我的項目沒有web.xml文件,我寧願不使用它。我也有使用Java日誌記錄機制的依賴項目。 – telebog

1

Spring Boot使用LoggingApplicationListener爲您的應用程序配置日誌記錄。此偵聽器是SpringApplication的默認偵聽器之一。要使用您自己的已配置日誌記錄系統,您需要配置您的SpringApplication,以便它沒有此偵聽器。例如,刪除不需要的聽衆,同時保留所有其他默認聽衆:

@SpringBootApplication 
public class CustomLoggingApplication { 

    public static void main(String[] args) {   
     SpringApplication application = 
       new SpringApplication(CustomLoggingApplication.class); 

     Collection<ApplicationListener<?>> listeners = 
       new ArrayList<ApplicationListener<?>>(); 
     for (ApplicationListener<?> listener: application.getListeners()) { 
      if (!(listener instanceof LoggingApplicationListener)) { 
       listeners.add(listener); 
      } 
     } 
     application.setListeners(listeners); 

     application.run(args); 
    } 

} 
+0

這段代碼對我的情況沒有影響 – telebog