如何在Spring项目中使用属性来配置log4j.xml

ans*_*ewe 6 logging spring log4j

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

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

小智 1

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

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());
    }
}
Run Code Online (Sandbox Code Playgroud)