[英]How to get a dynamic value from .properties file for log4j.xml
我正在使用log4j.xml文件。 我有两个环境,即实时和测试。 测试和实时的log4j的唯一区别是级别(INFO-live,DEBUG-test)。 所以我为两个env保留了常用的log4j.xml,并将该级别设为value = $ {loglevel}。 我已经定义了单独的live-log4j.properties,其中包含loglevel=INFO
。 同样也用于测试。 我的想法是,如果我为live构建jar,应该从live-log4j.properties文件中选择loglevel。 如果是测试版本,则从test-log4j.properties开始。
我的问题是,我做的是正确的吗? log4j.xml会从相应的* .properties文件中选择值吗?
我正在使用spring框架。 我已经提到了很多东西,但没有任何相关内容。 我在spring.xml中添加了这些属性文件(不确定它是否有效)我没有客户端连接来测试这个应用程序。
我的log4j版本:log4j 1.x
这是一篇帖子,解释了如何: 在applicationContext中的log4j配置 - forum.spring.io
你可以简化它
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:
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:
<root>
<level value="${webapp.logLevel}" />
</root>
live-log4j.properties:
logging.level=INFO
测试log4j.propertie:
logging.level=DEBUG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.