In log4j
, there is a feature configureAndWatch
(as mentioned below) where without application server restart, log threshold level can be modified with default delay.
org.apache.log4j.xml.DOMConfigurator.configureAndWatch(log4j.xml path);
Is this possible in log4j2
as well ? If yes, then how can this be achieved ?
Also, below are lines of code for setting up and cleaning up log4j
setup.
BasicConfigurator.configure()
BasicConfigurator.resetConfiguration()
How can this be achieved in log4j2
? Please help. Thanks.
You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.
Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config).
public class DOMConfigurator extends Object. Use this class to initialize the log4j environment using a DOM tree. The DTD is specified in log4j. dtd. Sometimes it is useful to see how log4j is reading configuration files.
Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.
Per Log4j2 documentation https://logging.apache.org/log4j/2.x/manual/configuration.html
When configured from a File, Log4j has the ability to automatically detect changes to the configuration file and reconfigure itself. If the monitorInterval attribute is specified on the configuration element and is set to a non-zero value then the file will be checked the next time a log event is evaluated and/or logged and the monitorInterval has elapsed since the last check. The example below shows how to configure the attribute so that the configuration file will be checked for changes only after at least 30 seconds have elapsed. The minimum interval is 5 seconds.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
...
</Configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With