2016-02-29 95 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