user1561017
user1561017

Reputation: 403

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

Upvotes: 1

Views: 2331

Answers (1)

user1561017
user1561017

Reputation: 403

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

Upvotes: 3

Related Questions