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

I have several property files in my Spring project. The Spring context loads these properties and handles the override property in a convenient way. Is there a way to use the properties available for my Spring XML configuration files (ie ${myprop} ) and use them in the same way in my log4j.xml file? I know that I can pass -Dprop=value system properties using -Dprop=value at startup, but I would prefer to have all the configuration in the properties files in my project. Is it possible?

My application runs on Tomcat.

+6
source share
2 answers

Try using this class after integrating multiple property files into one of the 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()); } } 
+1
source

I think the only way you can interact with Log4J is through the Nested Diagnostic Context .

So, if you are very desperate, you can write the Spring AOP aspect that sets the diagnostic context for your Spring Bean logs (and you can use them there). However, this will require the log to be accessible for Spring Bean, so you will need to add the getLog() method to your service interfaces (this becomes much easier if you use AspectJ static compilation and are described in AspectJ in action ).

But, without using AOP, I cannot imagine a reasonable way for Spring and Log4J to interact.

0
source

Source: https://habr.com/ru/post/889015/


All Articles