SpringMVC为每个构建环境加载不同的log4j文件
我使用的是SpringMVC3。今天我发现了spring概要文件以及如何在web.xml中设置它们。我正在从应用程序属性文件加载我的配置文件,但无法通过log4j实现相同的功能,以便根据我是为开发还是为产品构建来加载不同的log4j文件 以下是我为spring.profiles.active提供的内容SpringMVC为每个构建环境加载不同的log4j文件,spring,spring-mvc,log4j,Spring,Spring Mvc,Log4j,我使用的是SpringMVC3。今天我发现了spring概要文件以及如何在web.xml中设置它们。我正在从应用程序属性文件加载我的配置文件,但无法通过log4j实现相同的功能,以便根据我是为开发还是为产品构建来加载不同的log4j文件 以下是我为spring.profiles.active提供的内容 名为env.properties.的属性文件 spring.profiles.active=dev 实现ApplicationContextInitializer的AppInitializer类
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment environment = applicationContext.getEnvironment();
try {
ResourcePropertySource env = new ResourcePropertySource("classpath:env.properties");
String profile = (String)env.getProperty("spring.profiles.active");
environment.getPropertySources().addFirst(env);
...
}
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${spring.profiles.active}</param-value>
</context-param>
spring.profiles.active
${spring.profiles.active}
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j-${spring.profiles.active}.xml</param-value>
</context-param>
log4jConfigLocation
classpath:/log4j-${spring.profiles.active}.xml
目标是加载log4j-dev.xml,但这不起作用
有人能帮我吗
谢谢Log4j支持Log4j.xml文件中的变量替换。可以基于特定于环境的系统属性替换日志目录。这意味着您可以将文件名保留为:
<param-value>classpath:/log4j.xml</param-value>
这确实意味着您将使用系统属性而不是属性文件中的属性,但这可能是实现您所追求的目标的一种方式。或者,我如何使用属性文件中的值更改我的log4j中的日志目录?谢谢。所以log4j不允许您使用应用程序属性?据我所知。我总是用系统属性的方式来做这件事。还有另一种选择,即使用
-Dlog4j.configuration=file:/path/to/log4j.xml
属性,该属性允许您通过系统属性(而不是给定文件中的值)切换整个文件。
<param name="File" value="${log4j_dir}/filename.log" />
-Dlog4j_dir=/path/on/dev