Reload log4j2 configuration on demand
Asked Answered
K

4

27

I have my log4j2.xml config file set to be checked every 30 seconds:

<Configuration status="WARN" monitorInterval="30">
    ...
</Configuration>

Is it possible to programmatically tell log4j2 to check for changes in the configuration instead of having a timeout?

N.B. I don't want to programmatically load the configuration specifying the config file, I just want to tell log4j2 to check the config file that has been loaded before as if the monitorInterval expired.

Thanks!

Karenkarena answered 29/1, 2015 at 13:45 Comment(3)
See logging.apache.org/log4j/1.2/faq.html#a3.6Sleepyhead
@Sleepyhead I don't want to have log4j "watch" the config file... I just want to tell log4j when to checkKarenkarena
Please find my comment from another https://mcmap.net/q/534574/-how-to-reload-a-programmed-log4j2-configuration-in-javaVillous
K
33

It looks like I've found the solution:

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).reconfigure();

Does anyone see anything wrong/side-effects with this?

Karenkarena answered 30/1, 2015 at 12:7 Comment(3)
Unfortunately, I found this problemmatic when there are multiple logging contexts, like in OSGI or in web apps. In this case the code above reloads config in only one of the contextsThorman
@Thorman what would the solution for OSGI/web-apps environment?Stasiastasis
Sorry, @gaurav, already don't remember. Finally I preferred LogBackThorman
W
5

There is currently no clean way to do this. It can be done with reflection. (Of course this may break if the implementation changes.)

UPDATE: this is wrong. There is a clean way, see jamp's answer below.

org.apache.logging.log4j.core.LoggerContext ctx = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);

org.apache.logging.log4j.core.config.FileConfigurationMonitor mon = (org.apache.logging.log4j.core.config.FileConfigurationMonitor) ctx.getConfiguration().getConfigurationMonitor();

// use reflection to get monitor's "nextCheck" field.
// set field accessible
// set field value to zero

mon.checkConfiguration();
Wholly answered 30/1, 2015 at 1:57 Comment(3)
Oh. There is no API for doing what you want to do. I honestly believe reflection is your only option with log4j-2.1. You can of course raise a feature request on the Jira issue tracker.Wholly
It looks like you're right... can't find anything... :( Much to my regret, I'll accept it ;)Karenkarena
I take it back... I might have found a solution... I posted it as an answerKarenkarena
M
2

What about using org.apache.logging.log4j.core.config.Configurator ?

   LoggerContext loggerContext = LoggerContext.getContext(false);
   final Configuration configuration = loggerContext.getConfiguration();
   Configurator.reconfigure(configuration);
Midshipman answered 14/12, 2021 at 8:53 Comment(1)
just calling Configurator.reconfigure(); is work for meAliunde
B
0

What about org.apache.logging.log4j.core.config.Configurator.reconfigure()?

Broadleaf answered 12/7, 2023 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.