Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "configureAndWatch" in log4j2

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.

like image 261
sridhar Avatar asked Sep 29 '15 12:09

sridhar


People also ask

How do you change the log level in Log4j2 at runtime?

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.

How do I change the log4j configuration at runtime?

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).

What is DOMConfigurator in log4j?

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.

What is configuration status in Log4j2?

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.


1 Answers

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>
like image 171
alan7678 Avatar answered Oct 09 '22 22:10

alan7678