Java ConfigurationModelHandler-class And Method Code Example

The logback.classic.model.ConfigurationModelHandler class is a handler for the ConfigurationModel class in the logback library. It is responsible for managing the initialization of the ConfigurationModel and interpreting the data that's passed to it. Here's an example of how you might use the ConfigurationModelHandler class in a Java application:

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.model.ConfigurationModel;
import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.classic.model.LoggerModelHandler;
import ch.qos.logback.classic.model.RootLoggerModel;
import ch.qos.logback.classic.model.RootLoggerModelHandler;
import ch.qos.logback.classic.model.ContextModel;
import ch.qos.logback.classic.model.ContextModelHandler;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.spi.RuleStore;

public class MyApplication {
  public static void main(String[] args) {
    LoggerContext loggerContext = new LoggerContext();
    ConfigurationModel configurationModel = new ConfigurationModel();
    RuleStore rs = new RuleStore(loggerContext);
    // Adding handlers for different tags
    LoggerModelHandler loggerModelHandler = new LoggerModelHandler(loggerContext, configurationModel);
    RootLoggerModelHandler rootLoggerModelHandler = new RootLoggerModelHandler(loggerContext, configurationModel);
    ContextModelHandler contextModelHandler = new ContextModelHandler(loggerContext, configurationModel);
    rs.addRule(new ElementSelector("configuration"), configurationModel);
    rs.addRule(new ElementSelector("configuration/logger"), loggerModelHandler);
    rs.addRule(new ElementSelector("configuration/root"), rootLoggerModelHandler);
    rs.addRule(new ElementSelector("configuration/context"), contextModelHandler);
    // Installing the configuration
    try {
    } catch (JoranException e) {

In this example, a LoggerContext and ConfigurationModel are created. Then we are adding different handlers for the different tags of logback configuration such as logger,root,context. and install the configuration on loggerContext via ConfigurationModel.

As you can see, this example uses the RuleStore class which is used to hold the rules for processing the logback configuration file. addRule method is being used to add rules for different elements of configuration file, like for configuration, configuration/logger,configuration/root,configuration/context.

You can also load the configuration file by using JoranConfigurator class and parse it using the parse method.

It is worth noting that this is just a sample, you need to have a logback.xml or logback-test.xml file in your classpath or configure it manually.