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

本文是小编为大家收集整理的关于spring mvc为每个构建环境加载不同的log4j文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

这就是我对 spring.profiles.active 的看法.

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

    spring.profiles.active=dev

  2. 实现 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);
           ...
           }
    
  3. 我的 web.xml 中的以下内容

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

问题:现在我尝试加载一个基于 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

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

本文地址:https://www.itbaoku.cn/post/1574972.html

问题描述

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.

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

  1. A properties file named env.properties..

    spring.profiles.active=dev

  2. 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);
           ...
           }
    
  3. The following in my web.xml

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

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>

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

Can anyone help me with this please?

thanks

推荐答案

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.