2016-02-29 63 views
0

我有幾個Spring Boot應用程序部署在獨特的Tomcat中。 每個應用程序被配置成與包含客戶代碼Spring Boot:在logback配置中使用Tomcat上下文參數

<Context path="/myApp1" reloadable="false"> 
    <Parameter name="CUSTOMER_CODE" value="CUSTOMER1" /> 
</Context> 

我希望每個客戶具有基於在context.xml中定義的代碼單獨的日誌一個context.xml文件。

不幸的是這個配置並不在我的logback-config.xml中工作:

<property name="LOG_FILE" value="${ROOT_LOG}/${CUSTOMER_CODE}/myApp.log}"/> 

CUSTOMER_CODE_IS_UNDEFINED在 「ROOT_LOG」 目錄下創建一個文件夾。 「ROOT_LOG」由系統屬性提供。

有什麼辦法可以使這個logback配置工作嗎?

在application.properties中定義的屬性的使用效果很好(我將logback.xml重命名爲logback-spring.xml)。在我看來,在初始化日誌之前,Spring引導不會在Environnement中設置Tomcat上下文參數。任何想法的解決方法?謝謝。

+0

我覺得定製的logback配置(的logback -spring.xml)將執行您所需的操作:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration - 主要你可能只需要重命名你的配置文件。 – cjstehno

+0

感謝您的回答。我嘗試了logback-spring.xml,但沒有奏效。我更新了帖子以添加更多信息。 – ngu

回答

1

我終於找到了一個解決方案,在日誌初始化之前讓我的客戶代碼在Spring Environment bean中可用。不是很漂亮,但它的工作:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application extends SpringBootServletInitializer { 

    public static String APP_CUSTOMER_CODE; 

    /** 
    * Initialization of Spring Boot App with context param customer code 
    */ 
    @Override 
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder builder) { 
    final Map<String, Object> initProps = new HashMap<>(); 
    initProps.put("CUSTOMER_CODE", APP_CUSTOMER_CODE); 
    return builder.properties(initProps).sources(Application.class); 
    } 

    /** 
    * Method called before Spring Initialization 
    */ 
    @Override 
    public void onStartup(final ServletContext servletContext) throws ServletException { 
    APP_CUSTOMER_CODE = servletContext.getInitParameter("CUSTOMER_CODE"); 
    super.onStartup(servletContext); 
    } 

} 

在addidtion,我必須聲明的logback中,spring.xml一個springProperty標籤使用的變量:

<springProperty name="CUSTOMER_CODE" source="CUSTOMER_CODE"/> 
+0

我更新了帖子以接受我自己的答案 – ngu

相關問題