Spring MVC在每个构建环境中加载不同的log4j文件 [英] spring mvc loading different log4j file per build environment

查看:206
本文介绍了Spring MVC在每个构建环境中加载不同的log4j文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using spring mvc 3. Today I discovered spring profiles and how you can set them in your web.xml. I am loading my profile from an application properties file but cannot achieve the same thing with log4j in order to load a different log4j file based on whether or not I am building for dev or building for prod.

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

Here is what I have for spring.profiles.active.

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

  1. A properties file named env.properties..

spring.profiles.active = dev

spring.profiles.active=dev

一个实现ApplicationContextInitializer的AppInitializer类.

An AppInitializer class which implements ApplicationContextInitializer.

    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);
       ...
       }

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

  • The following in my web.xml

     <context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>${spring.profiles.active}</param-value>
     </context-param>
    

  • 问题:现在,我尝试像这样基于env加载另一个日志文件...

    Problem : Now I try and load a different log file based on env like so...

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

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

    Where the aim is to load log4j-dev.xml but this does not work.

    有人可以帮我吗?

    谢谢

    推荐答案

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

    Log4j supports variable substitution inside the log4j.xml file. You could substitute the log directory based on an environment specific system property. That would mean that you could keep the file name as:

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

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

    Then you could parameterize the directory inside the file:

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

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

    And then for each app server in each environment, you could set the appropriate folder path. Eg. for dev:

    -Dlog4j_dir=/path/on/dev
    

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

    It does mean that you'd be using a system property rather than a property in a properties file but it may be one way of achieving what you're after.

    这篇关于Spring MVC在每个构建环境中加载不同的log4j文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆