简体   繁体   中英

How to reconfigure log4j2 at runtime with new xml file

I'm using log4j2 in a Web Application (servlet 3.0 API)

Log4j2 is configuring itself automatically when the application is started using a log4j2.xml file inside the WEB-INF directory.

When the context is started I execute some logic that produce some logs, and all the log produced are working accordingly to the rules defined in the log4j2.xml file.

At the end of my initialization logic I call a service that gives to me one or more log4j2 xml configuration files (for example, config1.xml , config2.xml and config3.xml ).

Now I want to 'reconfigure' log4j using my initial log4j2.xml file together with all the new configuration files.

I cannot find a way to do this; I can find only a way to add programmatically new Appenders, new Loggers and so on;

I'd like to tell log4j2 "hey, here are some new configuration files; please, parse them and update your configuration accordingly"

There is a way to do this?

thanks

Thanks to Vionixt comment to my question, I solved the problem.

The code I used was:

System.setProperty("log4j.configurationFile", "<configFile1>,<configFile2>,<configFile3>");
LoggerContext.getContext(false).reconfigure();

where LoggerContext is

org.apache.logging.log4j.core.LoggingContext

After reconfiguration, all the new Logging rules were available and also used!

thank you so much, cheers

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