Spring MVC在每个构建环境中加载不同的log4j文件

我正在使用spring

mvc3。今天,我发现了spring配置文件,以及如何在web.xml中进行设置。我正在从应用程序属性文件中加载我的配置文件,但是无法基于log4j来实现相同的目的,从而无法根据我是为开发而构建还是为产品构建来加载不同的log4j文件。

这是我对spring.profiles.active的要求。

  1. 一个名为env.properties。的属性文件。

spring.profiles.active =开发

  1. 一个实现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);

    ...

    }

  2. 我的web.xml中的以下内容

     <context-param>

    spring.profiles.active

    ${spring.profiles.active}

问题:现在,我尝试像这样加载基于env的其他日志文件…

<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>classpath:/log4j-${spring.profiles.active}.xml</param-value>

</context-param>

目的是加载log4j-dev.xml,但这不起作用。

有人可以帮我吗?

谢谢

回答:

Log4j在log4j.xml文件中支持变量替换。您可以根据特定于环境的系统属性替换日志目录。这意味着您可以将文件名保留为:

<param-value>classpath:/log4j.xml</param-value>

然后,您可以参数化文件内的目录:

<param name="File" value="${log4j_dir}/filename.log" />

然后,对于每种环境中的每个应用服务器,您可以设置适当的文件夹路径。例如。对于开发人员:

-Dlog4j_dir=/path/on/dev

这确实意味着您将使用系统属性而不是属性文件中的属性,但这可能是实现目标的一种方式。

以上是 Spring MVC在每个构建环境中加载不同的log4j文件 的全部内容, 来源链接: utcz.com/qa/418114.html

回到顶部