Managing log file location through JNDI in Spring?

I have a Java web application that uses the SLF4J registration facade. Today we use the Log4J implementation under it (although we are considering switching to Logback). Log4J is currently configured through the log4j.xml configuration file, which is placed at the root of our class path.

In any case, we use JNDI to configure other aspects of our application, so I know very well how to install this and pull a string from JNDI into the Spring configuration file.

However, I do not understand how to create a Log4J application from the Spring configuration file. Even better, is it possible to fully configure Log4J through Spring and generally skip the log4j.xml configuration file? I hope I do not need to do this programmatically.

I found a Spring class called Log4jWebConfigurer, but this requires the WAR to start exploding (I don’t want me to be able to help it), and also that the log file is in the web application directory (definitely don’t want it).

+3
source share
1 answer

First get the main directory through JNDI:

<jee:jndi-lookup id="myAppHome" jndi-name="myAppHome" />

Then use this bean in the statement of the expression Spring expression as follows:

<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>#{ myAppHome + '/conf/log4j.xml'}</value>
        </list>
    </property>
</bean>
+2
source

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


All Articles