Spring 引导:在 logback 配置中使用 Tomcat 上下文参数
Spring Boot: Using Tomcat context parameters in logback configuration
我在一个独特的 Tomcat 中部署了多个 Spring 启动应用实例。
每个应用程序都配置了一个 context.xml 文件,其中包含一个客户代码
<Context path="/myApp1" reloadable="false">
<Parameter name="CUSTOMER_CODE" value="CUSTOMER1" />
</Context>
我希望每个客户都有一个基于 context.xml 中定义的代码的单独日志。
不幸的是,这个配置在我的 logback 中不起作用-config.xml:
<property name="LOG_FILE" value="${ROOT_LOG}/${CUSTOMER_CODE}/myApp.log}"/>
在 "ROOT_LOG" 目录中创建了一个文件夹 CUSTOMER_CODE_IS_UNDEFINED。 "ROOT_LOG" 由系统提供 属性.
有什么方法可以使这个 logback 配置起作用吗?
使用 application.properties 中定义的属性效果很好(我将 logback.xml 重命名为 logback-spring.xml)。在我看来,Spring 启动在初始化日志记录之前不会在环境中设置 Tomcat 上下文参数。任何解决方法的想法?谢谢
我终于找到了一个解决方案,可以在记录初始化之前让我的客户代码在 Spring 环境 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);
}
}
此外,我必须在 logback-spring.xml 中声明一个 springProperty 标签来使用变量:
<springProperty name="CUSTOMER_CODE" source="CUSTOMER_CODE"/>
我在一个独特的 Tomcat 中部署了多个 Spring 启动应用实例。 每个应用程序都配置了一个 context.xml 文件,其中包含一个客户代码
<Context path="/myApp1" reloadable="false">
<Parameter name="CUSTOMER_CODE" value="CUSTOMER1" />
</Context>
我希望每个客户都有一个基于 context.xml 中定义的代码的单独日志。
不幸的是,这个配置在我的 logback 中不起作用-config.xml:
<property name="LOG_FILE" value="${ROOT_LOG}/${CUSTOMER_CODE}/myApp.log}"/>
在 "ROOT_LOG" 目录中创建了一个文件夹 CUSTOMER_CODE_IS_UNDEFINED。 "ROOT_LOG" 由系统提供 属性.
有什么方法可以使这个 logback 配置起作用吗?
使用 application.properties 中定义的属性效果很好(我将 logback.xml 重命名为 logback-spring.xml)。在我看来,Spring 启动在初始化日志记录之前不会在环境中设置 Tomcat 上下文参数。任何解决方法的想法?谢谢
我终于找到了一个解决方案,可以在记录初始化之前让我的客户代码在 Spring 环境 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);
}
}
此外,我必须在 logback-spring.xml 中声明一个 springProperty 标签来使用变量:
<springProperty name="CUSTOMER_CODE" source="CUSTOMER_CODE"/>