1

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

4
  • Please check if "Composite Configuration" as described in the manual link works. Commented Nov 21, 2019 at 12:11
  • Unfortunately, that dosn't work. This is because the Composite Configuration can be used to specify more than one configuration file - and that's ok, but I need something more: I need to configure Log4j2 a second time, after the initial configuration happened... and I cannot find any API to do this Commented Nov 21, 2019 at 13:08
  • 1
    Chcek if the following helps. code ((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).reconfigure(); Commented Nov 22, 2019 at 8:34
  • Great!! It works. I've changed a bit the code to avoid the cast, using directly the org.apache.logging.log4j.core.LoggingContext API. I will responde to my question, pointing to your comments! ty Commented Nov 22, 2019 at 11:24

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

How did you reload your new configurations?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.