简体   繁体   English

如何从log4j.xml的.properties文件中获取动态值

[英]How to get a dynamic value from .properties file for log4j.xml

I am using log4j.xml file. 我正在使用log4j.xml文件。 I have two environments namely live & test. 我有两个环境,即实时和测试。 The only difference in the log4j for the test and live are the levels (INFO-live, DEBUG-test). 测试和实时的log4j的唯一区别是级别(INFO-live,DEBUG-test)。 So i have kept the common log4j.xml for both env and made the level as value=${loglevel}. 所以我为两个env保留了常用的log4j.xml,并将该级别设为value = $ {loglevel}。 I have defined separate live-log4j.properties which contains loglevel=INFO . 我已经定义了单独的live-log4j.properties,其中包含loglevel=INFO likewise for test as well. 同样也用于测试。 My idea is, if i build the jar for live, the loglevel should be picked from the live-log4j.properties file. 我的想法是,如果我为live构建jar,应该从live-log4j.properties文件中选择loglevel。 If for test build, then from test-log4j.properties. 如果是测试版本,则从test-log4j.properties开始。

Here my query is, am i doing the right thing? 我的问题是,我做的是正确的吗? will the log4j.xml will pick the values from the respective *.properties file? log4j.xml会从相应的* .properties文件中选择值吗?

I am using spring framework. 我正在使用spring框架。 I have referred lot of stuffs, but nothing is relevant to this. 我已经提到了很多东西,但没有任何相关内容。 I have added these properties files in the spring.xml (not sure if it work) I don't have client connectivity to test this app. 我在spring.xml中添加了这些属性文件(不确定它是否有效)我没有客户端连接来测试这个应用程序。

My log4j version: log4j 1.x 我的log4j版本:log4j 1.x

Here is a post that explain how to: log4j configuration in applicationContext - forum.spring.io 这是一篇帖子,解释了如何: 在applicationContext中的log4j配置 - forum.spring.io

you can simplify that 你可以简化它

ApplicationContext.xml: applicationContext.xml中:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>test-log4j.properties</value>
            <value>live-log4j.properties</value>
        </list>
    </property>
</bean>

<bean id="log4jDirectConfigurer" class="my.commons.logging.Log4jDirectConfigurer">
    <property name="location" value="classpath:log4j.xml"/>
    <property name="loglevelValue" value="${logging.level}"/>
</bean>

Log4jDirectConfigurer.java: Log4jDirectConfigurer.java:

package my.commons.logging;

import java.io.FileNotFoundException;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Log4jConfigurer;

public class Log4jDirectConfigurer implements InitializingBean 
{
    private String location;
    private String loglevelKey = "webapp.logLevel";

    public void setLocation( String location ){
        this.location = location;
    }

    public void setLoglevelValue( String loglevel ){
        String value = System.getProperty( loglevelKey );
        System.setProperty( loglevelKey, loglevel );
    }

    public void afterPropertiesSet() {
        if( location == null ){
            return;
        }

        try {
            Log4jConfigurer.initLogging( location );
        } catch (FileNotFoundException e) {
            System.out.println(location + ": File not found");
        }
    }
}

log4j.xml: 的log4j.xml:

<root>
    <level value="${webapp.logLevel}" />
</root>

live-log4j.properties: live-log4j.properties:

logging.level=INFO

test-log4j.propertie: 测试log4j.propertie:

logging.level=DEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM