Java ConfigurationWatchList-class And Method Code Example


The ConfigurationWatchList class from the logback.core package can be used to monitor a Logback configuration file for changes.

Here is an example of how the ConfigurationWatchList class could be used to monitor a Logback configuration file:

import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.ContextUtil;
import ch.qos.logback.core.util.Duration;
import ch.qos.logback.core.util.FileSize;
import ch.qos.logback.core.util.OptionHelper;

//...

String fileName = "logback.xml";
long threshold = FileSize.valueOf("10KB").getSize();
Duration delay = Duration.buildBySeconds(10);

Context context = new LoggerContext();
context = ContextUtil.bindContext(context);

try {
    ConfigurationWatchList configWatchList = ConfigurationWatchList.getConfigurationWatchList(context);
    configWatchList.setDelay(delay);
    configWatchList.setFileName(fileName);
    configWatchList.setThreshold(threshold);
    configWatchList.start();
} catch (JoranException e) {
    // handle exception
}

In this example, a ConfigurationWatchList object is created and initialized with the context, the configuration file name (logback.xml), the threshold file size and the delay time.

The setFileName() method is used to set the name of the configuration file to monitor. The setThreshold() method is used to set the minimum threshold file size for change detection. The setDelay() method is used to set the delay time between file size checks.

Finally, the start() method is called to start monitoring the configuration file.

This class is mainly used to monitor the configuration file and reload it if there's any change in the file. This can be very useful when you want to change log levels or other configuration options in a running application without having to restart it.

It's up to the developer to choose when and how to use this class, but it can be useful for monitoring the configuration file in production environments where you don't want to have to restart the application to apply changes to the configuration.

Please note that this class has dependency on logback core and logback classic library.