Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use properties in a Spring project to configure log4j.xml

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?

My app runs in Tomcat.

like image 540
anschoewe Avatar asked May 25 '11 16:05

anschoewe


1 Answers

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());
    }
}
like image 55
Bladean Mericle Avatar answered Oct 20 '22 02:10

Bladean Mericle