如何在Spring项目中使用属性来配置log4j.xml [英] How to use properties in a Spring project to configure log4j.xml

查看:648
本文介绍了如何在Spring项目中使用属性来配置log4j.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring项目中有多个属性文件. Spring上下文会加载这些属性,并以方便的方式处理属性覆盖.有没有办法获取我的Spring配置XML文件(即${myprop})可用的属性,并在我的log4j.xml文件中以类似的方式使用它们?我知道我可以在启动时使用-Dprop=value将系统属性传递给log4j,但我更希望将所有配置都包含在项目的属性文件中.这可能吗?

I have multiple properties files in my Spring project. The spring context loads these properties and handles property overriding in a convenient manner. Is there a way to take the properties that are available to my Spring configuration XML files (ie. ${myprop}) and use them in a similar fashion in my log4j.xml file? I know that I can pass system properties to log4j using -Dprop=value on startup, but I would prefer having all of the configuration in the properties files in my project. Is this possible?

我的应用程序在Tomcat中运行.

My app runs in Tomcat.

推荐答案

在将多个属性文件集成到一个属性后,尝试使用此类.

Try to use this class, after integrating your multiple properties files to one Properties.

public class DOMConfiguratorWithProperties extends DOMConfigurator {

    private Properties propertiesField = null;

    public synchronized Properties getProperties() {
        return propertiesField;
    }

    public synchronized void setProperties(final Properties properties) {
        propertiesField = properties;
    }

    @Override
    protected String subst(final String value) {
        return super.subst(value, getProperties());
    }

    public static void configure(final String filename) {
        new DOMConfiguratorWithProperties().doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }

    public static void configure(
            final String filename,
            final Properties properties) {
        DOMConfiguratorWithProperties configurator = new DOMConfiguratorWithProperties();
        configurator.setProperties(properties);
        configurator.doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }
}

这篇关于如何在Spring项目中使用属性来配置log4j.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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