简体   繁体   中英

How to set log4j path on startup in web app?

I have a log4j2.xml configuration file that should get the logging path dynamically set during startup of the web application by java code.

log4j2.xml:

<Properties>
    <property name="path">{web:attr.logpath}</property>
</Properties>

Therefore I have the following configuration file:

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class LogConfig extends SpringBootServletInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.addListener(Log4jConfigListener.class);
        servletContext.setAttribute("logpath", "d:/testpath");
        super.onStartup(servletContext);
    }
}

Problem: When onStartup() is entered, the log4j initialization is already over and I get the message that log4j path is invalid.

How can I delay the log4j initialization after the attributes have been set?

Spring uses logging. Log4j must initialize first or you end up with spring being unable to log. Normally this would be handled using Log4j's Log4jServletContextListener and setting logpath as config param in web.xml.

Please see http://logging.apache.org/log4j/2.x/manual/webapp.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM